Programmazione competitivaFacile

Salti minimi

Guida dettagliata e implementazione Python per il problema dei "Salti minimi".

Dichiarazione del problema

Facile

Scrivi una funzione min_jumps(arr) che trovi il numero minimo di salti per raggiungere l'ultimo indice di arr a partire dall'indice 0. Ogni elemento nell'array rappresenta la lunghezza massima del salto da quella posizione. Se è impossibile raggiungere l'ultimo indice, restituisce -1.

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

Esempi

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?
Prendi in considerazione l'utilizzo di strutture dati specifiche della programmazione dinamica 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.