Tensorum
YouTubeশেখা শুরু করুন
LeetCode 746 · 1D / Fibonacci

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]).

সহজArrayDynamic Programming
LeetCode-এ খুলুন ↗

আমি 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. 1

    State: dfs(i) / dp[i] = minimum cost paid to stand on step i.

  2. 2

    Base cases: steps 0 and 1 are free starting points, so their value is just their own cost.

  3. 3

    Transition: cost[i] + the cheaper of the two steps you could have jumped from.

  4. 4

    The top floor is beyond the last index — finish with min of the last two steps.

  5. 5

    Final version keeps only prev2 and prev instead of the full array.

আমি কীভাবে সমাধান করেছি · প্রথম ভাবনা থেকে চূড়ান্ত সমাধান

1
Recursion + memoO(n) time · O(n) space

dfs(index) with a dict cache, called for the last two steps.

2
TabulationO(n) time · O(n) space

dp array filled from index 2 forward.

3
Two variablesAcceptedO(n) time · O(1) space

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.