Skip to content

Instantly share code, notes, and snippets.

@shailrshah
Created September 29, 2017 23:32
Show Gist options
  • Save shailrshah/dc03999a7dc3de43433243b490fad4aa to your computer and use it in GitHub Desktop.
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.
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