Min Cost Climbing Stairs
LeetCode 746 · எளிது · தீர்வு, intuition & படிப்படியான வாக்த்ரூ
dp[i] = cost[i] + min(dp[i-1], dp[i-2]); answer = min(dp[n-1], dp[n-2]).
நான் Min Cost Climbing Stairs (LeetCode 746) ஐ இப்படித் தீர்த்தேன் — 1D / Fibonacci பேட்டர்னின் ஒரு எளிது பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.
கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.
Intuition
Think of dp[i] as the cheapest total to stand on step i having paid its cost. You can only arrive from i-1 or i-2, so take the cheaper of those and add cost[i]. The top is one step past the end, reachable from either of the last two steps.
Approach
- 1
State: dfs(i) / dp[i] = minimum cost paid to stand on step i.
- 2
Base cases: steps 0 and 1 are free starting points, so their value is just their own cost.
- 3
Transition: cost[i] + the cheaper of the two steps you could have jumped from.
- 4
The top floor is beyond the last index — finish with min of the last two steps.
- 5
Final version keeps only prev2 and prev instead of the full array.
நான் எப்படித் தீர்த்தேன் · முதல் யோசனையிலிருந்து இறுதித் தீர்வு வரை
dfs(index) with a dict cache, called for the last two steps.
dp array filled from index 2 forward.
prev2 / prev roll forward; the dp list is left unused.
Recursion + memoPythonO(n) time · O(n) space
class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
n = len(cost)
memo = {}
def dfs(index):
#if current index is at 1 or 0, we can reach the steps in cost[0]
#cost to reach the current index
#current step cost + min cost to reach previous steps
#cost[index]+dfs(index)
if index<0:
return 0
if index==0:
return cost[0]
if index == 1:
return cost[1]
if index in memo:
return memo[index]
cur_cost = cost[index]+min(dfs(index-1),dfs(index-2))
memo[index]=cur_cost
return memo[index]
#either I can start at last step/or either I can start at second last step
return min(dfs(n-1),dfs(n-2))TabulationPythonO(n) time · O(n) space
class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
n = len(cost)
dp = [float("inf")]*n
dp[0]=cost[0]
dp[1]=cost[1]
for index in range(2,n):
dp[index]=cost[index]+min(dp[index-1],dp[index-2])
return min(dp[n-1],dp[n-2])Two variablesPythonO(n) time · O(1) space
class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
n = len(cost)
dp = [float("inf")]*n
prev2=cost[0]
prev=cost[1]
"""
prev2 prev cur
prev2 prev
"""
for index in range(2,n):
cur=cost[index]+min(prev,prev2)
prev2 = prev
prev=cur
return min(prev,prev2)Min Cost Climbing Stairs — அடிக்கடி கேட்கப்படும் கேள்விகள்
Min Cost Climbing Stairs பின்னாலுள்ள intuition என்ன?
Think of dp[i] as the cheapest total to stand on step i having paid its cost. You can only arrive from i-1 or i-2, so take the cheaper of those and add cost[i]. The top is one step past the end, reachable from either of the last two steps.
Min Cost Climbing Stairs (LeetCode 746) ஐ எப்படித் தீர்ப்பது?
1. State: dfs(i) / dp[i] = minimum cost paid to stand on step i. 2. Base cases: steps 0 and 1 are free starting points, so their value is just their own cost. 3. Transition: cost[i] + the cheaper of the two steps you could have jumped from. 4. The top floor is beyond the last index — finish with min of the last two steps. 5. Final version keeps only prev2 and prev instead of the full array.
Min Cost Climbing Stairs இன் time மற்றும் space complexity என்ன?
Time: O(n). Space: O(1). (Accepted approach: Two variables.)
Min Cost Climbing Stairs இல் பொதுவான தவறுகள் என்ன?
The answer is not dp[n-1] alone — you can step to the top from n-2 as well. Starting at index 0 or 1 is free; don't add an extra step cost.
Min Cost Climbing Stairs ஐத் தீர்க்க ஒன்றுக்கு மேற்பட்ட வழிகள் உள்ளனவா?
Recursion + memo (O(n) time · O(n) space): dfs(index) with a dict cache, called for the last two steps. Tabulation (O(n) time · O(n) space): dp array filled from index 2 forward. Two variables (O(n) time · O(1) space): prev2 / prev roll forward; the dp list is left unused.