競賽程式設計簡單

最小跳躍

“最小跳轉”問題的詳細指南和 Python 實作。

問題陳述

簡單

寫一個函數 min_jumps(arr),找出從索引 0 開始到達 arr 最後一個索引的最小跳躍次數。陣列中的每個元素表示從該位置開始的最大跳轉長度。如果無法到達最後一個索引,則傳回-1。

約束條件
  • 1 <= len(arr) <= 10^4
  • 0 <= arr[i] <= 10^4

範例

Example 1
Input
min_jumps([1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9])
Output
3
Explanation

Jump from index 0 to 1 (value 3), then jump to index 4 (value 9), and then jump to the last index.

Example 2
Input
min_jumps([1, 1, 1, 1, 1])
Output
4
Explanation

Jump one by one from start to end, requiring 4 jumps.

Need a Hint?
考慮使用動態程式特定的資料結構,例如集合或堆。
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 資源

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