Count Unguarded Cells in the Grid (LeetCode 2257): Solution and Explanation
Guards see straight up, down, left and right until a wall or another guard blocks the view. Count the empty cells that no guard can see.
Intuition
A line of sight is just a walk along a row or column that stops at the first blocker. Only guards and walls block, so they must be on the grid before any sweep starts.
A cell that is already guarded does not block sight. The sweep keeps walking through it and only stops at a guard, a wall or the edge.
Because a guard stops another guard's view, in any one direction each cell is reached by at most one guard: the nearest guard behind it with no blocker in between. So every cell is visited at most four times, and the whole sweep is linear.
Approach: Sweep each guard's four lines of sight
- Create an m x n grid of 0s. Set guard cells to 2 and wall cells to 3.
- For each guard and each direction (up, down, left, right), step outward one cell at a time.
- While the cell is inside the grid and holds 0 or 1, set it to 1 (guarded) and keep going.
- Stop at a guard, a wall or the edge.
- Return the number of cells that are still 0.
Step-by-step example
m = 3, n = 3, guards = [[1,1]], walls = [[0,1],[1,0],[2,1],[1,2]]
- Place the guard at (1, 1) and walls at (0, 1), (1, 0), (2, 1) and (1, 2).
- Looking up, the very next cell (0, 1) is a wall, so nothing is marked.
- Down, left and right are also blocked right away by walls.
- No cell was marked guarded. The four corners (0, 0), (0, 2), (2, 0) and (2, 2) are still 0.
- Return 4.
Code: Python, Java, C++
1class Solution:
2 def countUnguarded(self, m: int, n: int, guards: List[List[int]], walls: List[List[int]]) -> int:
3 grid = [[0] * n for _ in range(m)]
4 for r, c in guards:
5 grid[r][c] = 2
6 for r, c in walls:
7 grid[r][c] = 3
8 for r, c in guards:
9 for dr, dc in ((-1, 0), (1, 0), (0, -1), (0, 1)):
10 i, j = r + dr, c + dc
11 while 0 <= i < m and 0 <= j < n and grid[i][j] < 2:
12 grid[i][j] = 1
13 i += dr
14 j += dc
15 return sum(row.count(0) for row in grid)1class Solution {
2 public int countUnguarded(int m, int n, int[][] guards, int[][] walls) {
3 int[][] grid = new int[m][n];
4 for (int[] g : guards) {
5 grid[g[0]][g[1]] = 2;
6 }
7 for (int[] w : walls) {
8 grid[w[0]][w[1]] = 3;
9 }
10 int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
11 for (int[] g : guards) {
12 for (int[] d : dirs) {
13 int i = g[0] + d[0], j = g[1] + d[1];
14 while (i >= 0 && i < m && j >= 0 && j < n && grid[i][j] < 2) {
15 grid[i][j] = 1;
16 i += d[0];
17 j += d[1];
18 }
19 }
20 }
21 int count = 0;
22 for (int[] row : grid) {
23 for (int v : row) {
24 if (v == 0) {
25 count++;
26 }
27 }
28 }
29 return count;
30 }
31}1class Solution {
2public:
3 int countUnguarded(int m, int n, vector<vector<int>>& guards, vector<vector<int>>& walls) {
4 vector<vector<int>> grid(m, vector<int>(n, 0));
5 for (auto& g : guards) {
6 grid[g[0]][g[1]] = 2;
7 }
8 for (auto& w : walls) {
9 grid[w[0]][w[1]] = 3;
10 }
11 int dirs[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
12 for (auto& g : guards) {
13 for (auto& d : dirs) {
14 int i = g[0] + d[0], j = g[1] + d[1];
15 while (i >= 0 && i < m && j >= 0 && j < n && grid[i][j] < 2) {
16 grid[i][j] = 1;
17 i += d[0];
18 j += d[1];
19 }
20 }
21 }
22 int count = 0;
23 for (auto& row : grid) {
24 for (int v : row) {
25 if (v == 0) {
26 count++;
27 }
28 }
29 }
30 return count;
31 }
32};Time and space complexity
O(m * n + g + w)
Placing guards and walls is O(g + w). In each direction a cell is swept by at most one guard, so all sweeps together touch O(m * n) cells, and the final count is one more pass.
O(m * n)
The grid stores one state per cell.
Other approaches
Four directional passes (time O(m * n + g + w), space O(m * n))
Scan every row left to right and right to left, and every column top to bottom and bottom to top, carrying a flag for whether a guard is in view. A wall clears the flag and a guard sets it. This avoids per-guard loops entirely.
Sweep without stopping at guards (time O(g * (m + n)), space O(m * n))
Stopping only at walls still gives the right answer, because a guard behind another guard sees the same cells. It can revisit long stretches many times, though, and becomes quadratic on rows full of guards.
Common mistakes
- Stopping the sweep at a cell that is already guarded. Guarded cells do not block sight, so later cells would be missed.
- Placing walls after starting sweeps. A guard processed early would see through walls that are not on the grid yet.
- Counting guard and wall cells as unguarded. Only unoccupied cells count.
Frequently asked questions
Why stop the sweep at another guard instead of walking past it?
The guard you hit will sweep that same direction itself, so everything beyond it gets marked anyway. Stopping there is what keeps each cell from being visited by many guards.
Do guards see diagonally?
No. Only the four cardinal directions count.
Is the grid too large to allocate?
No. m and n can each be up to 10^5, but their product is at most 10^5, so the grid has at most 10^5 cells.