forked from gouthampradhan/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClimbingStairs.java
More file actions
34 lines (28 loc) · 748 Bytes
/
ClimbingStairs.java
File metadata and controls
34 lines (28 loc) · 748 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package dynamic_programming;
/**
* Created by gouthamvidyapradhan on 01/04/2017.
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.
*/
public class ClimbingStairs
{
/**
* Main method
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception
{
}
public int climbStairs(int n)
{
if(n == 0 || n == 1) return 1;
int[] A = new int[n + 1];
A[n] = 1;
A[n - 1] = 1;
for(int i = n - 2; i >= 0; i --)
A[i] = A[i + 1] + A[i + 2];
return A[0];
}
}