Skip to content

Commit 05e9843

Browse files
authored
Create ClimbStairs.java
1 parent 227c326 commit 05e9843

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

ClimbStairs.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public int climbStairs(int n)
3+
{
4+
//same as fibonnacci series;
5+
//if(n==0 || n==1)
6+
// return 1;
7+
// return climbStairs(n-1) + climbStairs(n-2);
8+
9+
// above soln time limit exceeded
10+
// we can solve it by iteration or memoization or dyanamic programing
11+
if(n==0 ||n==1)
12+
return n;
13+
int step1=0;
14+
int step2=1,ans=0;
15+
for(int i=0;i<n;i++){
16+
ans=step1+step2;
17+
step1=step2;
18+
step2=ans;
19+
}
20+
return ans;
21+
22+
23+
24+
}
25+
}

0 commit comments

Comments
 (0)