Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions algorithms/java/ClimbingStairs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public int climbStairs(int n) {
// Create an array to store the number of ways to climb each stair.
int[] dp = new int[n + 1];

// There is one way to climb 0 stairs (no steps).
dp[0] = 1;

// There is one way to climb 1 stair (take 1 step).
dp[1] = 1;

// Calculate the number of ways to climb each stair from 2 to n.
for (int i = 2; i <= n; i++) {
// The number of ways to reach the current stair is the sum of
// the ways to reach the previous two stairs.
dp[i] = dp[i - 1] + dp[i - 2];
}

// The final value in dp array (dp[n]) contains the answer.
return dp[n];
}
}