Watch 5 video solutions for Create Grid With Exactly K Paths I, a medium level problem. This walkthrough by CodeSprint has 776 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given three integers m, n, and k.
Construct any m x n grid consisting only of the characters '.' and '#', where:
'.' represents a free cell.'#' represents an obstacle cell.A valid path is a sequence of free cells that:
(0, 0).(m - 1, n - 1).(i, j) to (i, j + 1), or(i, j) to (i + 1, j).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: m = 2, n = 3, k = 2
Output: ["...","#.."]
Explanation:

There are exactly k = 2 valid paths from (0, 0) to (1, 2):
(0, 0) → (0, 1) → (0, 2) → (1, 2)(0, 0) → (0, 1) → (1, 1) → (1, 2)Example 2:
Input: m = 3, n = 3, k = 4
Output: ["..#","...","#.."]
Explanation:

There are exactly k = 4 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)(0, 0) → (1, 0) → (1, 1) → (1, 2) → (2, 2)(0, 0) → (1, 0) → (1, 1) → (2, 1) → (2, 2)Example 3:
Input: m = 1, n = 4, k = 2
Output: []
Explanation:​
No grid exists with exactly k = 2 valid paths for a 1 x 4 grid, so the answer is an empty array.
Constraints:
1 <= m, n <= 101 <= k <= 4Problem Overview: You need to construct a grid where the number of valid paths from the start cell to the destination is exactly k. The challenge is not path traversal itself, but designing the grid so the path count matches the target value precisely.
Approach 1: Exhaustive Grid Construction (Exponential Time, Exponential Space)
The brute force strategy generates different grid layouts and computes the number of paths using DFS or dynamic programming after every modification. You iterate through combinations of blocked and open cells until the path count equals k. This approach demonstrates the underlying counting logic, but the search space grows exponentially as grid dimensions increase. It is mainly useful for validating small examples or debugging a more optimized construction strategy.
Approach 2: Dynamic Programming Path Counting (O(m * n) Time, O(m * n) Space)
Once a candidate grid is created, you can compute the number of valid paths using classic dynamic programming. Define dp[i][j] as the number of ways to reach cell (i, j). Each state accumulates values from the top and left neighbors while skipping blocked cells. This approach does not solve the construction problem alone, but it becomes the verification engine used by most accepted solutions. It is reliable when grid sizes are small enough to recompute path counts repeatedly.
Approach 3: Binary Construction with Controlled Paths (O(log k) Time, O(log k) Space)
The optimal solution treats the grid like a combinational structure where each branch contributes a known number of paths. You encode the value of k using carefully connected rows and columns so each split doubles or adds a fixed number of routes. The key insight is that path counts combine predictably, allowing you to represent large values using only a small grid. Most implementations build the structure incrementally using bit operations and verify the result with lightweight graph traversal or DP checks. This construction scales efficiently even when k is large.
Approach 4: DAG Interpretation of the Grid (O(V + E) Time, O(V) Space)
You can also model the grid as a directed acyclic graph where each open cell is a node and moves represent edges. Counting paths becomes a standard DAG DP problem processed in topological order. This perspective helps when reasoning about how adding or removing edges changes the total number of paths. It is especially useful in interviews because it connects the problem to reusable concepts from DP on graphs and path counting.
Recommended for interviews: Interviewers typically expect the constructive DP or binary-encoding approach because it shows you understand both path counting and controlled graph construction. Starting with brute force demonstrates intuition, but moving to logarithmic-size construction proves stronger algorithmic skill and complexity awareness.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Exhaustive Grid Construction | Exponential | Exponential | Small grids or debugging path logic |
| Dynamic Programming Verification | O(m * n) | O(m * n) | Counting paths in a fixed grid |
| Binary Constructive Design | O(log k) | O(log k) | General optimal solution for large k |
| DAG Path Counting | O(V + E) | O(V) | Graph-based reasoning and interview discussions |