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.