Create Grid With Exactly One Path - Solution & Explanation
Problem Statement
You are given two integers m and n, representing the number of rows and columns of a grid.
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:
- Starts at the top-left cell
(0, 0). - Ends at the bottom-right cell
(m - 1, n - 1). - 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 is exactly one valid path from the top-left cell to the bottom-right cell.
Example 1:
Input: m = 2, n = 3
Output: ["..#","#.."]
Explanation:

The only valid path is: (0,0) → (0,1) → (1,1) → (1,2)
Example 2:
Input: m = 3, n = 3
Output: ["..#","#..","##."]
Explanation:

The only valid path is: (0,0) → (0,1) → (1,1) → (1,2) → (2,2)
Example 3:
Input: m = 1, n = 4
Output: ["...."]
Explanation:
The only valid path is: (0,0) → (0,1) → (0,2) → (0,3)
Constraints:
1 <= m, n <= 25
Approach Overview
Problem Overview: You need to construct an m x n grid such that there is exactly one valid path from the top-left cell to the bottom-right cell. Movement is typically restricted to right or down, so the grid must block all alternative routes while keeping one continuous path.
Approach 1: Brute Force Grid Search (Backtracking) (Time: exponential, Space: O(m*n))
One theoretical approach is to generate different grid configurations of open and blocked cells and run a path counting algorithm such as DFS or DP to verify whether exactly one path exists. For every configuration, you run a traversal from (0,0) and count ways to reach (m-1,n-1). This quickly becomes infeasible because the number of possible grids grows exponentially with m*n. It mainly serves as a conceptual baseline showing the requirement: all alternative routes must be blocked.
Approach 2: Deterministic Grid Construction (Time: O(m*n), Space: O(1))
The practical solution is to construct the grid so only a single path is physically possible. A simple pattern is to keep the entire first row open and the entire last column open, while blocking all other cells. Starting from (0,0), the only possible movement is to go right across the first row until the last column, then move down to the bottom-right corner. Any attempt to move down earlier hits a blocked cell, eliminating alternative paths.
This guarantees exactly one valid route while keeping the implementation trivial. You initialize the grid, mark cells along the chosen path as passable, and block the rest. The algorithm only iterates through the grid once to assign values, so the runtime is O(m*n) with constant extra memory.
Conceptually, the problem relates to counting paths in a grid, commonly solved using dynamic programming. Instead of computing the number of paths, you design the grid so the count becomes exactly one. The construction also resembles a simple greedy strategy where you enforce a single deterministic route through a matrix.
Recommended for interviews: The deterministic construction approach. Interviewers expect you to recognize that multiple paths arise from branching choices. By blocking every branch except one straight route, you guarantee a unique path with a very simple O(m*n) construction. Mentioning brute force or DP path counting briefly shows understanding of the underlying grid path problem, but the direct construction demonstrates stronger problem-solving instincts.
Solution
We construct the grid as follows:
- First, construct a grid filled entirely with
#. - Set all elements in the first row to
.. - Set all elements in the last column to
.. - Return the constructed grid.
The time complexity is O(m times n), and the space complexity is O(m times n). Here, m and n are the number of rows and columns in the grid, respectively.
Code
Python
Java
C++
Go
TypeScript
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Grid Enumeration + DFS Path Count | Exponential | O(m*n) | Conceptual understanding of how unique paths emerge |
| Dynamic Programming Path Counting (Validation) | O(m*n) | O(m*n) | When verifying the number of paths in an existing grid |
| Deterministic Grid Construction | O(m*n) | O(1) | Best approach to directly build a grid with exactly one path |
Video Solution
3963. Create Grid With Exactly One Path (Leetcode Easy) • Programming Live with Larry • 297 views views
Watch 4 more video solutions →Frequently Asked Questions
Is Create Grid With Exactly One Path easy or hard?
Create Grid With Exactly One Path Python/Java solution
How to solve Create Grid With Exactly One Path in O(n)?
What is the best approach for Create Grid With Exactly One Path?
Is Create Grid With Exactly One Path asked at Google/Amazon/Meta?
What data structure is used in Create Grid With Exactly One Path?
What is the time complexity of Create Grid With Exactly One Path?
Ready to solve this problem?
Practice Create Grid With Exactly One Path with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor