October 27Easy
Create grid with exactly one path
Problem
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.
Solution
class Solution:
def createGrid(self, m: int, n: int) -> list[str]:
grid = [["#" for _ in range(n)] for _ in range(m)]
for i in range(min(m, n)):
grid[i][i] = "."
if i + 1 < n:
grid[i][i+1] = "."
if m > n:
for j in range(n, m):
grid[j][n-1] = "."
elif m < n:
for j in range(m+1, n):
grid[m-1][j] = "."
return ["".join(row) for row in grid]