Find Number of Ways to Reach the K-th Stair - Problem
You are given a non-negative integer k. There exists a staircase with an infinite number of stairs, with the lowest stair numbered 0.
Alice has an integer jump, with an initial value of 0. She starts on stair 1 and wants to reach stair k using any number of operations.
If she is on stair i, in one operation she can:
- Go down to stair
i - 1. This operation cannot be used consecutively or on stair0. - Go up to stair
i + 2^jump. And then,jumpbecomesjump + 1.
Return the total number of ways Alice can reach stair k.
Note: It is possible that Alice reaches stair k, and performs some operations to reach stair k again.
Input & Output
Example 1 — Basic Case
$
Input:
k = 1
›
Output:
2
💡 Note:
Alice starts at stair 1 (already at target). She can also go up to 2 then down to 1. Two ways total.
Example 2 — Multiple Paths
$
Input:
k = 2
›
Output:
2
💡 Note:
Path 1: Start at 1, go up by 2^0=1 to reach 2. Path 2: Start at 1, go up by 2^0=1 to 2, up by 2^1=2 to 4, down to 3, down to 2.
Example 3 — Edge Case
$
Input:
k = 0
›
Output:
1
💡 Note:
Alice starts at 1, can only go down to 0 (one way since she cannot go down consecutively).
Constraints
- 0 ≤ k ≤ 109
Visualization
Tap to expand
Understanding the Visualization
1
Starting Position
Alice starts at stair 1 with jump=0
2
Movement Rules
Up by 2^jump (jump++), Down by 1 (no consecutive)
3
Count Paths
Find all ways to reach target stair k
Key Takeaway
🎯 Key Insight: The exponentially growing jump sizes create predictable patterns that can be solved with combinatorial mathematics
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code