Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Climbing Stairs

Dynamic Programmingeasy

Implement `climbStairs(n)` returning the number of distinct ways to climb `n` stairs taking 1 or 2 steps at a time.

Sample tests

Input: climbStairs(2)
Output: 2
Input: climbStairs(3)
Output: 3

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Exponential recursion without memoization.

Learning resources

  • Wikipedia: Dynamic programming
Approach & explanation (try first)

Each step's count is the sum of the prior two, computed in O(n) time, O(1) space.

Loading...
⌘/Ctrl + Enter

Run your code to see results.