Grid Zone Coloring Feasibility
Trees & GraphsAuto-graded · 23 test casesLast updated
Determine whether empty grid plots can be assigned one of k colors without adjacent conflicts.
You are managing a rectangular field layout represented by a 2D matrix grid of size $m \times n$. Each cell in the matrix represents a zone:
0indicates an open plot that requires a crop assignment.-1indicates an unplantable obstacle or blocked space.
You have access to $k$ distinct crop types, represented by integers from $1$ to $k$. You must assign a crop type to every open plot (0) such that no two orthogonal (horizontally or vertically) adjacent open plots share the same crop type. Blocked spaces (-1) do not receive crops and do not create adjacency conflicts.
Write a function that determines whether a valid assignment exists for the given grid and integer $k$.
Constraints
- $m = \text{len}(grid)$
- $n = \text{len}(grid[0])$
- $1 \le m, n \le 10$
- $1 \le k \le 4$
grid[i][j]is either0or-1.
Examples
Example 1
Input: can_place_flowers([[0, 0], [0, -1]], 2)
Output: True
Example 2
Input: can_place_flowers([[0, 0], [0, 0]], 1)
Output: False
Example 3
Input: can_place_flowers([[0, -1], [-1, 0]], 1)
Output: True
Example 4
Input: can_place_flowers([[-1, -1], [-1, -1]], 1)
Output: True
Hints
Hint 1
Consider the graph formed by treating open plots (0) as nodes and orthogonal adjacencies as edges.
Hint 2
Any grid graph is bipartite. What is the chromatic number of a bipartite graph with at least one edge?
Hint 3
For k >= 2, can you always pattern the grid using alternating colors based on cell coordinates (i + j) % 2?
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem can be framed as graph coloring on the sub-graph formed by open plots (0), where edges represent horizontal or vertical adjacency.
A key property of 2D grid graphs is that they are bipartite—they contain no odd-length cycles. We can divide all cells into two sets based on the parity of their coordinates: (row + col) % 2 == 0 (even parity) and (row + col) % 2 == 1 (odd parity). Adjacent cells always have different parities.
Because of this bipartite structure:
- If $k \ge 2$: We can always achieve a valid coloring using just 2 colors (e.g., color 1 for even parity cells and color 2 for odd parity cells). Removing cells as obstacles (
-1) never introduces odd cycles, so $k \ge 2$ is always sufficient regardless of grid size or obstacle arrangement. - If $k = 1$: A valid assignment is possible if and only if no two open plots are orthogonally adjacent. If any pair of adjacent open plots exists, assigning them both color 1 creates a conflict. If no open plots are adjacent (or if there are no open plots at all), then using a single crop type works.
Algorithm
- If $k \ge 2$, immediately return
True. - If $k = 1$, iterate through the grid. If any open plot (
0) has an adjacent neighbor (right or down) that is also0, returnFalse. - If no adjacent
0pairs are found, returnTrue.
Complexity
- Time Complexity: $\mathcal{O}(m \times n)$ because we inspect each cell in the grid at most once when $k = 1$, and take $\mathcal{O}(1)$ time when $k \ge 2$.
- Space Complexity: $\mathcal{O}(1)$ as no extra memory or recursion stack is required.
from typing import List
def can_place_flowers(grid: List[List[int]], k: int) -> bool:
if k >= 2:
return True
rows = len(grid)
cols = len(grid[0])
for r in range(rows):
for c in range(cols):
if grid[r][c] != 0:
continue
if r + 1 < rows and grid[r + 1][c] == 0:
return False
if c + 1 < cols and grid[r][c + 1] == 0:
return False
return True