Create Grid With Exactly K Paths II Problem
Description
LeetCode Problem 3990.
You are given an integer k.
Construct any grid consisting only of the characters '.' and '#', where:
'.'represents a free cell.'#'represents an obstacle cell.
The grid must contain at most 25 rows and at most 25 columns.
A valid path is a sequence of free cells that:
- Starts at the top-left cell
(0, 0). - Ends at the bottom-right cell
(m - 1, n - 1), wheremandnare the dimensions of your constructed grid. - Moves only:
- Right, from
(i, j)to(i, j + 1), or - Down, from
(i, j)to(i + 1, j).
- Right, from
Return any grid such that there are exactly k valid paths from the top-left cell to the bottom-right cell. If no such grid exists, return an empty array.
Example 1:
Input: k = 2
Output: ["..#","#..","#.."]
Explanation:

The grid contains exactly 2 valid paths from (0, 0) to (2, 2):
- (0, 0) → (0, 1) → (1, 1) → (1, 2) → (2, 2)
- (0, 0) → (0, 1) → (1, 1) → (2, 1) → (2, 2)
Example 2:
Input: k = 3
Output: ["...","#..","#.."]
Explanation:

The grid contains exactly 3 valid paths from (0, 0) to (2, 2):
- (0, 0) → (0, 1) → (0, 2) → (1, 2) → (2, 2)
- (0, 0) → (0, 1) → (1, 1) → (1, 2) → (2, 2)
- (0, 0) → (0, 1) → (1, 1) → (2, 1) → (2, 2)
Constraints:
1 <= k <= 1000
Solution
Approach: Euclidean algorithm on a staircase of cells
Look at two neighbouring cells on one anti-diagonal that hold x and y paths. The cell between them on the next anti-diagonal is reachable from both, so it holds x + y paths. Keeping one more cell next to it, either the one above (which only sees x) or the one below (which only sees y), gives the next pair (x, x + y) or (x + y, y). Each step moves the pair one cell right or one cell down, and all the other cells stay obstacles.
Starting from the pair (1, 1) beside the start cell, a sequence of these steps reaches any coprime pair (x, y), and the final merging cell then holds x + y = k paths. It also lies in the bottom-right corner, because the pair only moves right and down. To find the steps, run the Euclidean algorithm backwards from (x, k - x): subtract the smaller number from the larger one until (1, 1) is reached. Try every x coprime to k and use the one with the fewest steps; for k <= 1000 this gives grids of at most 12 x 12. The case k = 1 is the single cell ".".
Complexity: time O(k log k), space O(1) besides the grid.
class Solution:
def createGrid(self, k: int) -> List[str]:
from math import gcd
if k == 1:
return ["."]
# Walk a pair of neighbouring anti-diagonal cells holding (x, y) paths. Each step adds the cell with
# x + y paths and keeps either x or y, which is one step of the Euclidean algorithm run backwards.
# Pick x with gcd(x, k - x) = 1 that needs the fewest steps.
best = None
for x in range(1, k):
if gcd(x, k) != 1:
continue
a, b = x, k - x
steps = []
while (a, b) != (1, 1):
if a > b:
a -= b
steps.append(1) # the pair moves down
else:
b -= a
steps.append(0) # the pair moves right
if best is None or len(steps) < len(best):
best = steps
ur, uc, lr, lc = 0, 1, 1, 0 # upper-right and lower-left cell of the pair
cells = [(0, 0), (0, 1), (1, 0)]
for s in reversed(best):
if s == 0:
uc += 1
lc += 1
else:
ur += 1
lr += 1
cells.append((ur, uc))
cells.append((lr, lc))
cells.append((lr, lc + 1))
m, n = lr + 1, lc + 2
g = [['#'] * n for _ in range(m)]
for r, c in cells:
g[r][c] = '.'
return [''.join(row) for row in g]