backbenchcode
← all problemsnext: Group by Signature
#07MediumAlgorithms · 55% acceptance

Counting Distinct Paths

Dynamic ProgrammingMath

You climb a staircase of `n` steps, taking 1 or 2 steps at a time. Return the number of distinct ways to reach the top.

The recurrence is the whole lesson: ways(n) = ways(n-1) + ways(n-2). Recognize it and DP stops being scary.

Examples

Input: n = 3

Output: 3

Note: 1+1+1, 1+2, and 2+1.

Input: n = 5

Output: 8

Constraints

Hints

solution.pyPython 3
1
2
3
4
5
Test console

// hit Run to execute the sample tests

// Interactive editor preview. Live code execution arrives with your early-access cohort.