Count Lattice Paths Through Required Checkpoints
Math & Bit ManipulationAuto-graded · 18 test casesLast updated
Tests whether a candidate can combine combinatorics (binomial coefficients) to count grid paths that must pass through a sequence of ordered checkpoints.
Count Lattice Paths Through Required Checkpoints
You are navigating an infinite 2-D grid, starting at the origin (0, 0). At each step you may move right (R) or up (U) — never left or down.
You are given a list of checkpoints: grid coordinates that your path must visit, in the order they are listed. The checkpoints are guaranteed to be reachable in order (each checkpoint is strictly to the right or above — or both — of the previous one, and the origin is implicitly the zeroth checkpoint).
Count the number of distinct monotone lattice paths from (0, 0) to the last checkpoint that pass through every checkpoint in the given order.
Because the answer can be very large, return it modulo 10^9 + 7.
How paths are counted
A monotone lattice path from point A = (r1, c1) to point B = (r2, c2) (with r2 >= r1 and c2 >= c1) consists of exactly (r2 - r1) + (c2 - c1) steps, of which (r2 - r1) are R-steps and (c2 - c1) are U-steps. The number of such paths is the binomial coefficient C(dx + dy, dx) where dx = r2 - r1 and dy = c2 - c1.
Because the checkpoints must be visited in order, the total count is the product of the path counts between consecutive checkpoints (including from the origin to the first checkpoint and from the last checkpoint to itself, which is trivially 1).
Input
checkpoints: a list of[x, y]pairs (non-empty), representing the required waypoints in order. The last checkpoint is the destination.- The path always starts at
(0, 0).
Constraints
1 <= len(checkpoints) <= 500 <= x, y <= 1000for every checkpoint[x, y]- Checkpoints are given in strictly increasing order: for consecutive checkpoints
[x1, y1]and[x2, y2], it holds thatx2 >= x1andy2 >= y1(with at least one strict inequality), and the first checkpoint satisfiesx >= 0,y >= 0(not both zero unless the only checkpoint is[0,0]). - The answer must be returned modulo 10^9 + 7.
Examples
Example 1
Input: count_checkpoint_paths([[2, 2]])
Output: 6
Example 2
Input: count_checkpoint_paths([[1, 0], [3, 2]])
Output: 6
Example 3
Input: count_checkpoint_paths([[1, 1], [3, 3], [5, 4]])
Output: 36
Hints
Hint 1
The number of monotone paths between two grid points depends only on the horizontal and vertical distances — look up or derive the formula involving a binomial coefficient.
Hint 2
Because checkpoints must be visited in the given order, the segments between consecutive checkpoints are independent, so multiply the per-segment path counts together.
Hint 3
Use Pascal's triangle or modular inverse-based factorials to compute large binomial coefficients under a prime modulus efficiently.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight
A monotone path (right/up only) from (x1, y1) to (x2, y2) is fully determined by choosing which steps are rightward among all dx + dy steps. That is exactly C(dx + dy, dx) paths, where dx = x2 - x1 and dy = y2 - y1.
Because the checkpoints are visited in a fixed order, each segment of the path (from one checkpoint to the next) is independent of all other segments. Therefore the total number of valid paths is simply the product of the per-segment counts.
Algorithm
- Prepend the implicit starting point
(0, 0). - For each consecutive pair of waypoints compute
dx,dy, thenC(dx + dy, dx). - Multiply all these binomial coefficients together modulo
10^9 + 7.
Modular Arithmetic
Since coordinates go up to 1000, the maximum dx + dy per segment is 2000. We precompute factorials and their modular inverses up to 2000 using Fermat's little theorem (the modulus 10^9 + 7 is prime), giving O(1) per binomial coefficient query.
Correctness
- Single checkpoint
[0,0]:dx = dy = 0,C(0,0) = 1. One trivial path (stay put). Correct. - Collinear checkpoints: if
dy = 0,C(dx, 0) = 1— only one path along the x-axis. Correct. - Multiple checkpoints: independence of segments ensures multiplication is the right operation.
Complexity
- Time: O(MAX_N + n) where
n = len(checkpoints)andMAX_N = 2000. Effectively O(1) per test given fixed constraints. - Space: O(MAX_N) for factorial tables.
from math import comb
def count_checkpoint_paths(checkpoints: list) -> int:
MOD = 10**9 + 7
MAX_N = 2001
fact = [1] * (MAX_N + 1)
for i in range(1, MAX_N + 1):
fact[i] = fact[i - 1] * i % MOD
inv_fact = [1] * (MAX_N + 1)
inv_fact[MAX_N] = pow(fact[MAX_N], MOD - 2, MOD)
for i in range(MAX_N - 1, -1, -1):
inv_fact[i] = inv_fact[i + 1] * (i + 1) % MOD
def mod_comb(n, r):
if r < 0 or r > n:
return 0
return fact[n] * inv_fact[r] % MOD * inv_fact[n - r] % MOD
result = 1
prev_x, prev_y = 0, 0
for cp in checkpoints:
cx, cy = cp[0], cp[1]
dx = cx - prev_x
dy = cy - prev_y
result = result * mod_comb(dx + dy, dx) % MOD
prev_x, prev_y = cx, cy
return result