3963. Create Grid With Exactly One Path
1 min read
LeetCode Daily Problem: Create Grid With Exactly One Path
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).
Return any grid such that there is exactly one valid path from the top-left cell to the bottom-right cell.
Nothing to explain here — here is the code.
python
class Solution:
def createGrid(self, m: int, n: int) -> list[str]:
return ['.'+'#'*(n-1)]*(m-1) + ['.'*n]Every row except the last has a single free cell on the left, so the robot is forced downward. The last row is completely free, so it moves right to the end. That gives exactly one path.
Complexity
- Time: O(mn) to build the output.
- Space: O(mn) for the output.
SeriesDaily Problem →
TagsSimulation