Le migliori 150 intervisteMedio

Salire le scale

Guida dettagliata e implementazione Python per il problema 'Salire le scale'.

Dichiarazione del problema

Medio

Stai salendo una scala. Ci vogliono n passi per raggiungere la cima.

Ogni volta puoi salire 1 o 2 gradini. In quanti modi distinti puoi raggiungere la vetta?

Scrivi una funzione climbStairs(n: int) -> int.

Vincoli
  • 1 <= n <= 45

Esempi

Example 1
Input
n = 2
Output
2
Explanation

There are two ways to climb: 1+1 steps, or 2 steps.

Example 2
Input
n = 3
Output
3
Explanation

There are three ways: 1+1+1 steps, 1+2 steps, or 2+1 steps.

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche per DP 1D 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.