Count Grid Walkways Without Three Consecutive Steps
Dynamic ProgrammingAuto-graded · 21 test casesLast updated
Count valid grid paths composed of horizontal and vertical steps such that no direction is repeated three times in a row.
You are navigating a 2D grid from the origin (0, 0) to the destination (right_moves, up_moves). Each move must be either one step to the right (R) or one step upward (U).
A path is considered valid if it never contains three consecutive identical moves (that is, neither RRR nor UUU appears anywhere in the path sequence).
Write a function count_grid_paths(right_moves: int, up_moves: int) -> int that calculates the total number of distinct valid paths from (0, 0) to (right_moves, up_moves).
Rules & Edge Cases
- If either
right_movesorup_movesis negative, return0. - If both
right_movesandup_movesare0, return1(the empty path is valid). - A path consisting of only one step direction is valid up to 2 steps (e.g.,
(2, 0)returns1), but invalid for 3 or more steps (e.g.,(3, 0)returns0).
Constraints
-100 <= right_moves <= 200-100 <= up_moves <= 200
Examples
Example 1
Input: count_grid_paths(2, 2)
Output: 6
Example 2
Input: count_grid_paths(3, 1)
Output: 2
Example 3
Input: count_grid_paths(0, 2)
Output: 1
Hints
Hint 1
Standard grid DP tracks path counts at cell (i, j), but here the next move depends on recent moves. Consider adding state tracking for the last direction taken and its current streak length.
Hint 2
Since a streak cannot reach 3, the current streak length of the last move can only be 1 or 2. How many total state variables per cell does this require?
Hint 3
An 'R' step from (i-1, j) can transition from any state at (i-1, j) ending in a 'U' streak (giving streak length 1), or from a state ending in 'R' with streak length 1 (giving streak length 2).
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
In a standard grid path counting problem, the number of ways to reach cell (i, j) is simply the sum of paths from (i-1, j) and (i, j-1). However, because we must enforce the restriction that no direction is taken 3 times in a row (RRR or UUU), the state must keep track of which direction was taken last and how long that consecutive run is.
Since runs of length 3 or more are disallowed, the length of the current run can only be 1 or 2. Thus, for each cell (i, j) representing i right steps and j up steps used so far, we keep four distinct counts:
r1: Paths arriving at(i, j)viaRwith anR-streak of length 1 (previous move wasUor start).r2: Paths arriving at(i, j)viaRwith anR-streak of length 2 (previous move wasR1).u1: Paths arriving at(i, j)viaUwith aU-streak of length 1 (previous move wasRor start).u2: Paths arriving at(i, j)viaUwith aU-streak of length 2 (previous move wasU1).
State Transitions
When calculating values for (i, j):
- Moving Right (R):
cur_r1[j] = prev_u1[j] + prev_u2[j](Switching direction toRstarts a new streak of 1).cur_r2[j] = prev_r1[j](Extending an existingRstreak of length 1 to 2).- (Extending
prev_r2would create a streak of length 3, which is disallowed and omitted).
- Moving Up (U):
cur_u1[j] = cur_r1[j - 1] + cur_r2[j - 1](Switching direction toUstarts a new streak of 1).cur_u2[j] = cur_u1[j - 1](Extending an existingUstreak of length 1 to 2).
Base Cases
- If both
right_moves == 0andup_moves == 0, return1. - If either coordinate is negative, return
0. - The first step from
(0, 0)is initialized specifically:(1, 0)gets 1 way in stater1, and(0, 1)gets 1 way in stateu1.
Complexity Analysis
- Time Complexity: $\mathcal{O}(R \cdot U)$ where $R = \text{right_moves}$ and $U = \text{up_moves}$. Each cell is processed in constant time using simple additions.
- Space Complexity: $\mathcal{O}(U)$ by storing only the previous and current row of DP values (or $\mathcal{O}(\min(R, U))$ if swapped).
Python Reference Solution
def count_grid_paths(right_moves: int, up_moves: int) -> int:
if right_moves < 0 or up_moves < 0:
return 0
if right_moves == 0 and up_moves == 0:
return 1
width = up_moves + 1
prev_r1 = [0] * width
prev_r2 = [0] * width
prev_u1 = [0] * width
prev_u2 = [0] * width
for i in range(right_moves + 1):
cur_r1 = [0] * width
cur_r2 = [0] * width
cur_u1 = [0] * width
cur_u2 = [0] * width
for j in range(width):
cur_r1[j] = prev_u1[j] + prev_u2[j]
if i == 1 and j == 0:
cur_r1[j] += 1
cur_r2[j] = prev_r1[j]
if j > 0:
cur_u1[j] = cur_r1[j - 1] + cur_r2[j - 1]
cur_u2[j] = cur_u1[j - 1]
elif i == 0:
cur_u1[j] = 0
if i == 0 and j == 1:
cur_u1[j] += 1
prev_r1, prev_r2, prev_u1, prev_u2 = cur_r1, cur_r2, cur_u1, cur_u2
return prev_r1[up_moves] + prev_r2[up_moves] + prev_u1[up_moves] + prev_u2[up_moves]