Climbing Stairs in Java (LeetCode 70): DP Approach
The Climbing Stairs problem (LeetCode 70) is one of the classic introductory problems for mastering Dynamic Programming (DP). The premise is simple: you are climbing a staircase with n steps, and you can take either 1 step or 2 steps at a time. The goal is to determine the total number of distinct ways to reach the top.
In this guide, we'll break down the intuition, trace the pattern, build both the array-based DP solution and an optimal $O(1)$ space solution in Java.
Prefer watching? Here is the complete video walkthrough:
1. Problem Statement & Examples
You are climbing a staircase. It takes n steps to reach the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Example 1:
Input: n = 2
Output: 2
Explanation: There are two ways to climb to the top:
1. 1 step + 1 step
2. 2 steps
Example 2:
Input: n = 3
Output: 3
Explanation: There are three ways to climb to the top:
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step
Example 3:
Input: n = 4
Output: 5
Explanation:
1. 1 + 1 + 1 + 1
2. 1 + 1 + 2
3. 1 + 2 + 1
4. 2 + 1 + 1
5. 2 + 2
dp[i-1]). Coral = 2 steps below (dp[i-2]).
2. The Fibonacci Recurrence Intuition
To reach step i, think about where your previous position had to be:
- You came from step (i - 1) and took a single 1-step move.
- Or you came from step (i - 2) and took a single 2-step move.
There are no other ways to land on step i. Therefore, the total ways to reach step i is simply the sum of both:
dp[i] = dp[i - 1] + dp[i - 2]
Notice the sequence formed by the answers:
n = 1→ 1 wayn = 2→ 2 waysn = 3→ 3 waysn = 4→ 5 waysn = 5→ 8 waysn = 6→ 13 ways
This is precisely the Fibonacci Sequence ($1, 2, 3, 5, 8, 13\dots$).
3. Java Implementation (DP Array - $O(n)$ Space)
Important Edge Case: Always check if n <= 2 before allocating the array. If n = 1 and you initialize dp[2] = 2, you will get an ArrayIndexOutOfBoundsException!
class Solution {
public int climbStairs(int n) {
// Base cases
if (n <= 2) {
return n;
}
// DP table
int[] dp = new int[n + 1];
dp[1] = 1;
dp[2] = 2;
for (int i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
}
4. Space Optimization ($O(1)$ Space)
Notice that to calculate dp[i], we only ever need the values of the last two steps (prev1 and prev2). We don't actually need to store the entire array of history.
class Solution {
public int climbStairs(int n) {
if (n <= 2) return n;
int prev2 = 1; // ways to reach step 1
int prev1 = 2; // ways to reach step 2
for (int i = 3; i <= n; i++) {
int current = prev1 + prev2;
prev2 = prev1;
prev1 = current;
}
return prev1;
}
}
5. Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| DP Array | O(n) | O(n) |
| Space Optimized | O(n) | O(1) (Constant) |
6. Summary & Takeaways
- Always identify whether a problem can be broken down into sub-problems. Here, reaching step
ndepends strictly on stepn-1andn-2. - Avoid re-computing subproblems by either caching them in an array or rolling variables forward.
- Always test base conditions (
n = 1,n = 2) to prevent array indexing runtime exceptions.
Practice on LeetCode: LeetCode 70 – Climbing Stairs
Comments
Post a Comment