150強訪談簡單

跳躍遊戲二

“Jump Game II”問題的詳細指南和 Python 實作。

問題陳述

簡單

給定一個 0 索引的長度為 n 的整數 nums 陣列。您最初位於 nums[0]。每個元素 nums[i] 表示從索引 i 向前跳躍的最大長度。返回到達 nums[n - 1] 的最小跳躍次數。產生測試案例以便您可以到達最後一個索引。

寫一個函數 jump(nums: List[int]) -> int

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

範例

Example 1
Input
nums = [2,3,1,1,4]
Output
2
Explanation

Jump 1 step from index 0 to 1, then 3 steps to the last index.

Example 2
Input
nums = [2,3,0,1,4]
Output
2
Explanation

Jump 1 step from index 0 to 1, then 3 steps to the last index.

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 資源

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