Skip to content

Commit 3178771

Browse files
committed
solve climbing stairs
1 parent 34a6520 commit 3178771

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

climbing-stairs/samcho0608.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
// Problem:
3+
// * can take 1 or 2 steps
4+
// * return: how many distinct ways to climb to the top(n)
5+
// Solution:
6+
// * Time Complexity: O(N)
7+
// * due to memoization(DP)
8+
// * Space Complexity: O(N)
9+
public int climbStairs(int n) {
10+
// memo[i] = distinct steps to reach ith step
11+
int[] memo = new int[n + 1];
12+
memo[0] = 1;
13+
memo[1] = 1;
14+
15+
for(int i = 2; i < n+1; i++) {
16+
memo[i] = memo[i-1] + memo[i-2];
17+
}
18+
19+
return memo[n];
20+
}
21+
}

0 commit comments

Comments
 (0)