InferenceAI

Enter at least two characters to search.

Start free

Staircase Ways

easyDynamic Programming15 min10 XP

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 count satisfies W(n)=W(n−1)+W(n−2)W(n) = W(n-1) + W(n-2) with W(0)=W(1)=1W(0) = W(1) = 1.

Constraints

  • 0≤n≤900 \le n \le 90

Examples

EXAMPLE WALKTHROUGHtwo ways

A guided view of the published example, not a trace of your code.

INPUT[2]
TASKstaircase_ways()
EXPECTED OUTPUT2

Start with the input values shown in this example.

Example 1 of 3

Input: n = 2

Output: 2

Explanation: two ways

Input: n = 3

Output: 3

Explanation: three ways

Input: n = 0

Output: 1

Explanation: zero steps

Hint 1

You only need the previous two values, not the whole table.

Topics

DSADynamic ProgrammingPython