Le migliori 150 intervisteFacile

Gioco di salto

Guida dettagliata e implementazione Python per il problema "Jump Game".

Dichiarazione del problema

Facile

Ti viene fornito un array di numeri interi. Inizialmente sei posizionato sul primo indice dell'array e ogni elemento nell'array rappresenta la lunghezza massima del salto in quella posizione.

Restituisce True se riesci a raggiungere l'ultimo indice, altrimenti False.

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

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

Esempi

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

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

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

You will always arrive at index 3. Its maximum jump length is 0, which makes it impossible to reach 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.