Watch 7 video solutions for Find Number of Ways to Reach the K-th Stair, a hard level problem involving Math, Dynamic Programming, Bit Manipulation. This walkthrough by codestorywithMIK has 3,888 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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:
i - 1. This operation cannot be used consecutively or on stair 0.i + 2jump. And then, jump becomes jump + 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:
Example 2:
Input: k = 1
Output: 4
Explanation:
The 4 possible ways of reaching stair 1 are:
Constraints:
0 <= k <= 109Problem Overview: You start from stair 1 and perform a sequence of constrained moves to reach stair k. The challenge is counting how many valid sequences of operations end exactly at k. Because moves grow exponentially and have restrictions on consecutive steps, the solution relies on math, combinatorics, and efficient state checks rather than brute‑force enumeration.
Approach 1: Sorting and Two-Pointer Technique (Time: O(n log n), Space: O(1) or O(n))
Generate all candidate stair positions that can appear during valid sequences. These positions come from applying the allowed jump pattern and optional step-backs. Once you have the candidate values, sort them and use the two‑pointer technique to count combinations that end exactly at k. One pointer starts from the smallest reachable state while the other scans from the largest; adjust pointers based on whether the current sum or transformation overshoots or undershoots k. Sorting costs O(n log n), while the two‑pointer scan runs in O(n). This method is useful when the candidate states are precomputed and you want a deterministic scan without extra memory overhead.
Approach 2: HashSet for Complement Check (Time: O(n), Space: O(n))
Instead of sorting, store generated stair states in a HashSet. For each state, compute the complement or transformation needed to reach k. A constant‑time hash lookup checks whether the complementary state exists. This reduces the search phase to linear time, giving O(n) complexity after generating the candidate states. The trade‑off is additional memory usage for the hash structure. Hash‑based lookups work well when the number of candidate states grows and sorting would become the bottleneck.
Both strategies rely on understanding how the allowed moves produce valid stair numbers. That reasoning often comes from dynamic programming or direct mathematical observation of the jump pattern. Once the reachable states are known, the problem becomes an efficient counting task.
Recommended for interviews: Interviewers typically expect a math‑driven observation that reduces the search space first. After identifying the reachable stair states, a linear solution using a hash structure is usually preferred because it achieves O(n) lookup time and demonstrates familiarity with efficient counting techniques. Discussing the sorted two‑pointer method first can still help show problem‑solving progression before presenting the optimal hash‑based approach.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sorting + Two-Pointer Technique | O(n log n) | O(1)–O(n) | When candidate states can be sorted and scanned deterministically with minimal extra memory |
| HashSet for Complement Check | O(n) | O(n) | Best for fast lookups when checking if a complementary state exists |