Find Number of Ways to Reach the K-th Stair Problem
Description
LeetCode Problem 3154.
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 stair 0. - Go up to stair
i + 2^jump. And then,jumpbecomesjump + 1.
Return the total number of ways Alice can reach stair k.
Note that it is possible that Alice reaches the stair k, and performs some operations to reach the stair k again.
Example 1:
Input: k = 0
Output: 2
Explanation:
The 2 possible ways of reaching stair 0 are:
- Alice starts at stair 1.
- Using an operation of the first type, she goes down 1 stair to reach stair 0.
- Alice starts at stair 1.
- Using an operation of the first type, she goes down 1 stair to reach stair 0.
- Using an operation of the second type, she goes up 2^0 stairs to reach stair 1.
- Using an operation of the first type, she goes down 1 stair to reach stair 0.
Example 2:
Input: k = 1
Output: 4
Explanation:
The 4 possible ways of reaching stair 1 are:
- Alice starts at stair 1. Alice is at stair 1.
- Alice starts at stair 1.
- Using an operation of the first type, she goes down 1 stair to reach stair 0.
- Using an operation of the second type, she goes up 2^0 stairs to reach stair 1.
- Alice starts at stair 1.
- Using an operation of the second type, she goes up 2^0 stairs to reach stair 2.
- Using an operation of the first type, she goes down 1 stair to reach stair 1.
- Alice starts at stair 1.
- Using an operation of the first type, she goes down 1 stair to reach stair 0.
- Using an operation of the second type, she goes up 2^0 stairs to reach stair 1.
- Using an operation of the first type, she goes down 1 stair to reach stair 0.
- Using an operation of the second type, she goes up 2^1 stairs to reach stair 2.
- Using an operation of the first type, she goes down 1 stair to reach stair 1.
Constraints:
0 <= k <= 10^9
Solution
Approach: Memoized Search
Let dfs(i, j, jump) be the number of ways to end on stair k when standing on stair i, where j = 1 if the last move was a step down and jump is how many up-moves were made so far (the next up-move is 2^jump). The answer is dfs(1, 0, 0).
- If
i > k + 1, return 0: we cannot step down twice in a row, so we can never come back tok. - If
i == k, count one way and keep going, because we may leave and return. - If
i > 0and the last move was not a step down, we may go down: adddfs(i - 1, 1, jump). - We may always go up: add
dfs(i + 2^jump, 0, jump + 1).
Memoizing on (i, j, jump) keeps the number of states small.
Complexity: O(log2 k) time and space.
from functools import lru_cache
class Solution:
def waysToReachStair(self, k: int) -> int:
@lru_cache(None)
def dfs(i: int, j: int, jump: int) -> int:
if i > k + 1:
return 0
ans = int(i == k)
if i > 0 and j == 0:
ans += dfs(i - 1, 1, jump)
ans += dfs(i + (1 << jump), 0, jump + 1)
return ans
return dfs(1, 0, 0)