Flower Placement
GoogleOnsiteHard
A garden is a square grid given as grid, a list of n strings of n characters each. Each character is 'H' for a house or '0' for an empty cell. Two cells are neighbours when they share an edge. You may plant a flower in any empty cell.
Part 1: Check that a placement exists
Plant flowers in empty cells so that all of these hold:
- Each row and each column contains at most one flower.
- Each house has exactly one flower among its neighbours.
- Each flower has exactly one house among its neighbours, so a flower never serves two houses and is never planted away from every house.
Return true if a valid placement exists, and false otherwise. A grid with no houses has a valid placement with no flowers.
- Example 1
- grid
["H00","000","00H"]- Output
true
Why: Put one flower at row 0, column 1 (next to the top-left house) and one at row 1, column 2 (next to the bottom-right house). They share no row or column, so a valid placement exists.
- Example 2
- grid
["HH","00"]- Output
false
Why: The top-left house can use only the cell below it and the top-right house can use only the cell below it, and those two cells are in the same row. A row holds at most one flower, so there is no valid placement.
Constraints
1 <= n <= 6len(grid[i]) == ngrid[i][j]is'H'or'0'