Climbing Stairs - Complete Solution Guide
Climbing Stairs is LeetCode problem 70, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 Constraints: 1 <= n <= 45
Detailed Explanation
You're tasked with counting the distinct ways to reach the top of a staircase with `n` steps. The catch is that you can only take either 1 or 2 steps at a time. This isn't about finding the shortest path (which would always be `n/2` two-steps if possible), but rather about enumerating *all unique sequences* of 1s and 2s that sum up to `n`. For instance, to reach `n=3`, `(1, 2)` and `(2, 1)` are distinct sequences, even though they use the same steps in different orders, because the order of steps matters.
Solution Approach
The provided solution tackles "Climbing Stairs" using an iterative dynamic programming approach, essentially building up the solution from the ground. The key insight is to realize that the number of ways to reach stair `i` can only be derived from the number of ways to reach stair `i-1` (by taking a 1-step) or the number of ways to reach stair `i-2` (by taking a 2-step). There are no other possibilities for your final move to stair `i`.
Step-by-Step Algorithm
- Step 1: Initialize a DP array `dp` of size `n+1`. Set `dp[1] = 1` and `dp[2] = 2` as base cases.
- Step 2: Iterate from `i = 3` to `n`. For each `i`, calculate `dp[i] = dp[i-1] + dp[i-2]`.
- Step 3: Return `dp[n]`, which holds the total number of ways to reach the nth step.
Key Insights
- **Additive Recurrence Relation:** The number of distinct ways to reach stair `i` is determined solely by the sum of ways to reach stair `i-1` (taking a 1-step) and ways to reach stair `i-2` (taking a 2-step). This `dp[i] = dp[i-1] + dp[i-2]` relationship directly mirrors the Fibonacci sequence, forming the heart of the solution.
- **Bottom-Up DP for Efficiency:** A direct recursive solution would re-evaluate the same subproblems (`climbStairs(k)`) multiple times, leading to an exponential explosion of calls. Storing the results for `dp[k]` as they are computed iteratively in a bottom-up DP (like the provided solution) prevents this redundancy, collapsing the complexity to linear time.
- **Correct Base Case Initialization:** Establishing the initial conditions `dp[1] = 1` and `dp[2] = 2` is fundamental. These aren't arbitrary; they accurately reflect the distinct ways for the smallest `n` values (1 step: `[1]`; 2 steps: `[1, 1]` or `[2]`) from which the entire Fibonacci-like sequence builds. Without correctly defining these "seed" values, the entire recurrence would yield incorrect results.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Math, Dynamic Programming, Memoization.
Companies
Asked at: AMD, Accenture, Accolite, Adobe, Amazon, Apple, Barclays, Bloomberg, Bolt, ByteDance, Cisco, Citadel, Deloitte, Disney, Expedia, Goldman Sachs, Grammarly, IBM, Infosys, Intel, Intuit, J.P. Morgan, Meta, Microsoft, Nvidia, Oracle, Qualcomm, TikTok, Turing, Uber, Wipro, Yahoo, Yandex, Zoho, tcs.