Problem · Dynamic Programming

Climb Stairs with One, Two, or Three Steps

Learn this problem
EasyAgoda logoAgodaFULLTIMEONSITE INTERVIEW

Problem statement

A staircase has n steps. Starting before the first step, each move climbs exactly 1, 2, or 3 steps.

Return the exact number of ordered move sequences that land exactly on step n. For n = 0, return 1 for the empty sequence.

Function

countStairWays(n: int) → long

Examples

Example 1

n = 4return = 7

The seven sequences are 1+1+1+1, 1+1+2, 1+2+1, 2+1+1, 2+2, 1+3, and 3+1.

Example 2

n = 0return = 1

There is one way to climb zero steps: take no moves.

Constraints

  • 0 <= n <= 50
  • The exact answer fits in a signed 64-bit integer.

More Agoda problems

drafts saved locally
public long countStairWays(int n) {
    // Write your solution here.
}
n4
expected7
checking account