150強訪談中等

爬樓梯的最低成本

「最小成本爬樓梯」問題的詳細指南和 Python 實作。

問題陳述

中等

給定一個整數數組 cost,其中 cost[i] 是樓梯上第 i 步的成本。支付費用後,您可以爬一級或兩級台階。

您可以從索引 0 的步驟開始,也可以從索引 1 的步驟開始。

返回到達樓層頂部的最低成本。

寫一個函數 minCostClimbingStairs(cost: List[int]) -> int

約束條件
  • 2 <= len(cost) <= 1000
  • 0 <= cost[i] <= 999

範例

Example 1
Input
cost = [10,15,20]
Output
15
Explanation

Start at index 1, pay 15, and climb to the top. Total is 15.

Example 2
Input
cost = [1,100,1,1,1,100,1,1,100,1]
Output
6
Explanation

Start at index 0, pay 1, climb to 2, pay 1, climb to 4, pay 1, climb to 6, pay 1, climb to 7, pay 1, climb to 9, pay 1, climb to top. Total is 6.

Need a Hint?
考慮使用一維 DP 特定的資料結構,例如集合或堆。
Edge Cases to Watch
  • 空輸入結構
  • 單元素輸入
  • 大數值範圍

準備好解決了嗎?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

在編輯器中開啟
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推薦的 Python 資源

透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。