Skip to main content
  1. LeetCode/

LeetCode 746: Min Cost Climbing Stairs

·2 mins· ·
LeetCode Easy Dynamic-Programming
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2026-04-25 Source: Day 10 learning note

Study Context
#

This article is rebuilt from the exact LeetCode section in the learning note. I kept the note’s repair points, comparison points, and common mistakes, while removing unrelated non-LeetCode material from the same day.

Related Reminders From The Note#

    1. Re-solve LC 746 Min Cost Climbing Stairs cleanly.

Learning Note Extract
#

Problem 2 - LC 746 Min Cost Climbing Stairs
#

  • Status: Good enough.
  • Pattern: Fibonacci-style minimum-cost DP.

State
#

dp[i] = minimum cost to reach step i

Base Case
#

dp[0] = cost[0]
dp[1] = cost[1]

Transition
#

dp[i] = cost[i] + min(dp[i - 1], dp[i - 2])

Final Answer
#

The top is one step beyond the last index, so:

answer = min(dp[n - 1], dp[n - 2])

Complexity
#

Time: O(n)
Space: O(n)

Interview-Ready Explanation
#

I can start from step 0 or step 1. To reach step i, I must come from i - 1 or i - 2, so the minimum cost to reach i is the current step cost plus the cheaper of those two previous states. Since the top is beyond the last step, the answer is the cheaper of reaching the last or second-last step.

Clean Solution
#

The note above captures the reasoning and the mistakes to avoid. The implementation below is the version I would submit.

from typing import List

class Solution:
    def minCostClimbingStairs(self, cost: List[int]) -> int:
        prev2 = prev1 = 0
        for i in range(2, len(cost) + 1):
            curr = min(prev1 + cost[i - 1], prev2 + cost[i - 2])
            prev2, prev1 = prev1, curr
        return prev1

Complexity
#

Time O(n), Space O(1).

Mistakes To Watch
#

  • Paying cost for the top floor, which has no cost.
  • Off-by-one between stair index and step position.

Final Interview Explanation
#

Start from the state definition, then explain why the transition preserves that state. If there is a loop direction, state compression, or a similar-looking problem with a different answer shape, call that out explicitly because that is where this problem family usually breaks down.

Related

LeetCode 70: Climbing Stairs
·2 mins
LeetCode Easy Dynamic-Programming
LeetCode note for Climbing Stairs, rebuilt from the original learning note
LeetCode 322: Coin Change
·2 mins
LeetCode Medium Dynamic-Programming Unbounded-Knapsack
LeetCode note for Coin Change, rebuilt from the original learning note
LeetCode 740: Delete and Earn
·2 mins
LeetCode Medium Dynamic-Programming
LeetCode note for Delete and Earn, rebuilt from the original learning note
LeetCode 213: House Robber II
·2 mins
LeetCode Medium Dynamic-Programming
LeetCode note for House Robber II, rebuilt from the original learning note
LeetCode 1971: Find if Path Exists in Graph
·2 mins
LeetCode Easy Graph Union-Find
LeetCode note for Find if Path Exists in Graph, rebuilt from the original learning note
LeetCode 496: Next Greater Element I (Monotonic Stack)
·2 mins
LeetCode Daily Easy Array Stack Monotonic-Stack Hash-Map Next-Greater-Element
Monotonic decreasing stack over nums2 to build next-greater map; answer queries for nums1.