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.