Le migliori 150 intervisteFacile

Salta il gioco II

Guida dettagliata e implementazione Python per il problema 'Jump Game II'.

Dichiarazione del problema

Facile

Ti viene fornito un array con indice 0 di numeri interi di lunghezza n. Inizialmente sei posizionato su nums[0]. Ogni elemento nums[i] rappresenta la lunghezza massima di un salto in avanti dall'indice i. Restituisce il numero minimo di salti per raggiungere nums[n - 1]. I casi di test vengono generati in modo tale da poter raggiungere l'ultimo indice.

Scrivi una funzione jump(nums: List[int]) -> int.

Vincoli
  • 1 <= len(nums) <= 10^4
  • 0 <= nums[i] <= 1000

Esempi

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?
Prendi in considerazione l'utilizzo di strutture dati specifiche di Greedy come set o heap.
Edge Cases to Watch
  • Strutture di input vuote
  • Ingressi a elemento singolo
  • Grandi limiti numerici

Pronto a risolvere?

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

Apri nell'editor
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

Risorse Python consigliate

Espandi le tue conoscenze con tutorial interattivi, foglietti illustrativi e confronti di codici correlati.