Top 150 Interview

Climbing Stairs

Detailed guide and Python implementation for the 'Climbing Stairs' problem.

問題提起

You are climbing a staircase. It takes n steps to reach the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

Write a function climbStairs(n: int) -> int.

制約
  • 1 <= n <= 45

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?
Consider using 1D DP-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

解決する準備はできましたか?

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 リソース

関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。