Skip to content

Commit 3a443a4

Browse files
committed
add: ClimbingStairsBottomUp
1 parent 54ee678 commit 3a443a4

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class ClimbingStairsBottomUp {
2+
public int climbingStairsBottomUp(int n) {
3+
if (n <= 2) {
4+
return n;
5+
}
6+
int[] dp = new int[n + 1];
7+
// Base cases.
8+
dp[1] = 1;
9+
dp[2] = 2;
10+
// Starting from step 3, calculate the number of ways to reach each
11+
// step until the n-th step.
12+
for (int i = 3; i < n + 1; i++) {
13+
dp[i] = dp[i - 1] + dp[i - 2];
14+
}
15+
return dp[n];
16+
}
17+
}

0 commit comments

Comments
 (0)