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.