Created
September 29, 2017 23:32
-
-
Save shailrshah/dc03999a7dc3de43433243b490fad4aa to your computer and use it in GitHub Desktop.
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? Note: Given n will be a positive integer.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Stairs { | |
static HashMap<Integer, Integer> hm; | |
public int climbStairs(int n) { | |
hm = new HashMap<>(); | |
hm.put(0, 1); | |
return helper(n); | |
} | |
private int helper(int n) { | |
if(n < 0) | |
return 0; | |
if(hm.containsKey(n)) | |
return hm.get(n); | |
else { | |
hm.put(n, helper(n-1) + helper(n-2)); | |
} | |
return hm.get(n); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment