Climbing Stairs
LeetCode 70 · आसान · समाधान, intuition और कदम-दर-कदम वॉकथ्रू
ways(n) = ways(n-1) + ways(n-2), with ways(0) = ways(1) = 1.
मैंने Climbing Stairs (LeetCode 70) ऐसे हल किया — 1D / Fibonacci पैटर्न का एक आसान प्रॉब्लम। नीचे: intuition, कदम-दर-कदम approach, मेरा accepted Python कोड, animated test cases और line-by-line debugger।
नीचे का विवरण अंग्रेज़ी में है।
Intuition
To land on step n you must have come from step n-1 (a 1-step) or step n-2 (a 2-step). So the number of ways to reach n is just the ways to reach n-1 plus the ways to reach n-2 — the Fibonacci recurrence wearing a staircase costume.
Approach
- 1
Define the state: ways(i) = number of distinct ways to stand on step i.
- 2
Base cases: there is exactly one way to be on step 0 or step 1.
- 3
Transition: the last move was either a 1-step or a 2-step, so add the two smaller answers.
- 4
Top-down: recurse from n and cache each index in a memo so every step is solved once.
- 5
Bottom-up: fill dp[2..n] left to right, then notice each value only needs the previous two and keep just two variables.
मैंने कैसे हल किया · पहले विचार से अंतिम हल तक
dfs(index) recurses down to the base cases and caches results in a dict.
Fill a dp array from the bottom step upward.
Slide left_prev and prev forward — the dp array collapsed to two cells.
Recursion + memoPythonO(n) time · O(n) space
class Solution:
def climbStairs(self, n: int) -> int:
memo = {}
def dfs(index):
if index<=1:
return 1
if index in memo:
return memo[index]
cost = dfs(index-1)+dfs(index-2)
memo[index]=cost
return cost
return dfs(n)TabulationPythonO(n) time · O(n) space
class Solution:
def climbStairs(self, n: int) -> int:
dp = [0]*(n+1)
dp[0]=1
dp[1]=1
for index in range(2,n+1):
cost = dp[index-1]+dp[index-2]
dp[index]=cost
return dp[n]Two variablesPythonO(n) time · O(1) space
class Solution:
def climbStairs(self, n: int) -> int:
"""
left_prev prev. cur
left_prev prev
"""
left_prev=1
prev=1
for index in range(2,n+1):
cur = prev+left_prev
left_prev=prev
prev = cur
return prevClimbing Stairs — अक्सर पूछे जाने वाले प्रश्न
Climbing Stairs के पीछे intuition क्या है?
To land on step n you must have come from step n-1 (a 1-step) or step n-2 (a 2-step). So the number of ways to reach n is just the ways to reach n-1 plus the ways to reach n-2 — the Fibonacci recurrence wearing a staircase costume.
Climbing Stairs (LeetCode 70) कैसे हल करें?
1. Define the state: ways(i) = number of distinct ways to stand on step i. 2. Base cases: there is exactly one way to be on step 0 or step 1. 3. Transition: the last move was either a 1-step or a 2-step, so add the two smaller answers. 4. Top-down: recurse from n and cache each index in a memo so every step is solved once. 5. Bottom-up: fill dp[2..n] left to right, then notice each value only needs the previous two and keep just two variables.
Climbing Stairs की time और space complexity क्या है?
Time: O(n)। Space: O(1) in the final version (O(n) for memo / table)। (Accepted approach: Two variables।)
Climbing Stairs में आम गलतियाँ क्या हैं?
Plain recursion without the memo is exponential. Start the loop at 2 — dp[0] and dp[1] are seeds, not computed values.
क्या Climbing Stairs को हल करने के एक से ज़्यादा तरीके हैं?
Recursion + memo (O(n) time · O(n) space): dfs(index) recurses down to the base cases and caches results in a dict. Tabulation (O(n) time · O(n) space): Fill a dp array from the bottom step upward. Two variables (O(n) time · O(1) space): Slide left_prev and prev forward — the dp array collapsed to two cells.