↓快轉到主要內容
  1. LeetCode/

LeetCode 746: Min Cost Climbing Stairs

·2 分鐘· ·
LeetCode Easy Dynamic-Programming
Wei Yi Chung
作者
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
目錄

基本資料
#

難易度: easy 第一次嘗試:2026-04-25 來源:Day 10 learning note

學習脈絡
#

這篇是從 learning note 裡該 LeetCode 題目的段落重新整理出來的版本。我保留當天筆記中的修正點、比較點、容易犯錯的地方,並移除同一天其他非 LeetCode 主題,避免文章內容混題。

筆記中提到的相關提醒
#

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

當天筆記摘錄
#

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.

正確解法
#

上面的筆記保留了推理脈絡和當天需要修正的點。下面是我會提交的版本。

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

複雜度
#

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

要特別避免的錯誤
#

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

面試口說整理
#

先講清楚 state definition,再說 transition 為什麼維持這個 state。只要這題有 loop direction、狀態壓縮、或題型相似但 answer shape 不同的地方,就要主動講出來,因為那通常就是這類題最容易出錯的點。

相關文章

LeetCode 70: Climbing Stairs
·2 分鐘
LeetCode Easy Dynamic-Programming
LeetCode 70 解題筆記,依照原始 learning note 重新整理
LeetCode 338: Counting Bits
·1 分鐘
LeetCode Easy Dynamic-Programming Bit-Manipulation
LeetCode 338 解題筆記:dp[i] = dp[i » 1] + (i & 1),或 dp[i] = dp[i - 最高位的 2 的次方] + 1
LeetCode 1137: N-th Tribonacci Number
·1 分鐘
LeetCode Easy Math Dynamic-Programming Memoization
LeetCode 1137 解題筆記:跟 Fibonacci 一樣,用三個變數滾動;小心 T0 = 0、T1 = T2 = 1 的起始值和迴圈範圍
LeetCode 1025: Divisor Game
·1 分鐘
LeetCode Easy Math Dynamic-Programming Brainteaser Game-Theory
LeetCode 1025 解題筆記:dp[i] = 存在 i 的因數 x 讓 dp[i - x] 是輸;觀察後答案就是 n 是偶數
LeetCode 509: Fibonacci Number
·1 分鐘
LeetCode Easy Math Dynamic-Programming Recursion Memoization
LeetCode 509 解題筆記:一維 DP 的起點:只需要前兩項,用兩個變數滾動
LeetCode 392: Is Subsequence
·1 分鐘
LeetCode Easy Two-Pointers String Dynamic-Programming
LeetCode 392 解題筆記:兩個指針,t 的指針一直往前,對上 s 的字元時 s 的指針也前進;兩邊的邊界都要檢查