Skip to content

Instantly share code, notes, and snippets.

@AnjaliManhas
Created May 10, 2020 11:04
Show Gist options
  • Save AnjaliManhas/c53bd0296e8da268d18a8012c33cf71d to your computer and use it in GitHub Desktop.
Save AnjaliManhas/c53bd0296e8da268d18a8012c33cf71d to your computer and use it in GitHub Desktop.
Climbing Stairs- LeetCode: 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?
class Solution {
public int climbStairs(int n) {
int[] no_of_ways = new int[n + 1];
//step zero
no_of_ways[0] = 1;
//step one
no_of_ways[1] = 1;
// from 2nd step it is sum of previous two
for (int j = 2; j <= n; j++) {
no_of_ways[j] = no_of_ways[j - 1] + no_of_ways[j - 2];
}
return no_of_ways[n];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment