Create Grid With Exactly K Paths II - Solution & Explanation
Problem Statement
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
Approach Overview
Problem 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 precisely.
Approach 1: Brute Force Enumeration (Exponential Time, Exponential Space)
The most direct idea is to generate candidate grids and count all possible paths using DFS or backtracking. For each grid configuration, recursively move right or down and increment the count whenever you reach the destination. This works only for very small constraints because the number of grid states and path combinations grows exponentially. Time complexity is O(2^(m*n)) or worse depending on the search space, and space complexity is O(m*n) for recursion.
Approach 2: Dynamic Programming Path Counting (O(m*n) Time, O(m*n) Space)
A better strategy computes the number of paths for a fixed grid using dynamic programming. Define dp[r][c] as the number of ways to reach cell (r,c). Each value comes from the top and left neighbors. This approach efficiently validates whether a constructed grid produces exactly k paths. You typically combine this with incremental grid modification or state search. This is a standard application of dynamic programming and grid traversal.
Approach 3: Binary Construction / DAG Encoding (Optimal Construction)
The optimal solution treats the grid as a directed acyclic graph where each open cell contributes to the total number of reachable paths. The key insight is that path counts can represent powers of two, allowing you to encode any value of k through selective blocking or enabling of transitions. Instead of brute forcing layouts, you build a structured grid where each layer doubles the number of possible routes. Then you use the binary representation of k to activate only the required contributions. This reduces construction complexity dramatically and scales even for very large values of k.
Most accepted solutions use a compact construction with carefully placed walls and open cells. The implementation relies on graph traversal concepts and sometimes bit manipulation to map binary digits into path choices. Typical time complexity is O(log k) or O(n^2) depending on the grid size used for encoding, while space complexity remains O(n^2).
Recommended for interviews: Interviewers usually expect the constructive DP or binary encoding approach. Starting with brute force shows you understand how paths are counted, but the optimized construction demonstrates algorithmic reasoning and control over combinatorial growth. Focus on explaining how each grid layer contributes a deterministic number of paths and how binary decomposition guarantees an exact match for k.
Solutions for this problem are being prepared.
Try solving it yourselfDetailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Enumeration | O(2^(m*n)) | O(m*n) | Small grids or validating ideas during debugging |
| DP Path Counting | O(m*n) | O(m*n) | When the grid is fixed and you only need path counts |
| Binary Grid Construction | O(log k) | O(n^2) | General optimal solution for large k values |
| DAG-Based Layer Construction | O(n^2) | O(n^2) | When deterministic path composition is required |
Frequently Asked Questions
Is Create Grid With Exactly K Paths II easy or hard?
Create Grid With Exactly K Paths II Python/Java solution
How to solve Create Grid With Exactly K Paths II in O(n)?
What is the best approach for Create Grid With Exactly K Paths II?
Is Create Grid With Exactly K Paths II asked at Google/Amazon/Meta?
What data structure is used in Create Grid With Exactly K Paths II?
What is the time complexity of Create Grid With Exactly K Paths II?
Ready to solve this problem?
Practice Create Grid With Exactly K Paths II with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor