Back to posts

LeetCode Challenge Day 48 β€” 2257. Count Unguarded Cells in the Grid

Nitin Ahirwal / November 2, 2025

LeetCode ChallengeDay 48GridSimulationMediumJavaScriptPortfolio

Hey folks πŸ‘‹

This is Day 48 of my LeetCode streak πŸš€
Today’s problem is 2257. Count Unguarded Cells in the Grid β€” a medium-level simulation problem where guards protect cells in four directions until blocked by walls or other guards.


πŸ“Œ Problem Statement

You are given two integers m and n representing a grid of size m x n.
You are also given two arrays:

  • guards[i] = [row_i, col_i] β†’ the position of the ith guard
  • walls[j] = [row_j, col_j] β†’ the position of the jth wall

Each guard can see every cell in the same row and column until a wall or another guard blocks the view.

Return the number of unguarded cells in the grid.


πŸ’‘ Intuition

The main idea is to simulate how guards protect cells.
Each guard can watch cells in four directions β€” up, down, left, and right β€” until blocked by walls or other guards.

So, if we can mark all the cells that are guarded, the number of remaining unmarked cells gives the count of unguarded ones.


πŸ”‘ Approach

  1. Initialize a grid of size m x n, where:

    • 0 β†’ empty cell
    • 1 β†’ guard
    • 2 β†’ wall
    • 3 β†’ guarded cell
  2. Mark all walls and guards in the grid.

  3. For each guard, sweep in four directions:

    • Continue marking cells as 3 (guarded) until a wall (2) or another guard (1) blocks the way.
  4. After all guards have been processed, count how many cells remain with value 0 β€” these are unguarded.


⏱️ Complexity Analysis

  • Time complexity:
    (O(m \times n)) β€” Each cell is visited a limited number of times during sweeps.

  • Space complexity:
    (O(m \times n)) β€” For maintaining the grid state.


πŸ§‘β€πŸ’» Code (JavaScript)

/**
 * @param {number} m
 * @param {number} n
 * @param {number[][]} guards
 * @param {number[][]} walls
 * @return {number}
 */
var countUnguarded = function(m, n, guards, walls) {
    // 0 = empty, 1 = guard, 2 = wall, 3 = guarded
    const grid = Array.from({length: m}, () => new Array(n).fill(0));
    
    // mark walls
    for (const [wr, wc] of walls) {
        grid[wr][wc] = 2;
    }
    // mark guards
    for (const [gr, gc] of guards) {
        grid[gr][gc] = 1;
    }
    
    // helper to sweep in a direction from a guard
    const sweep = (r, c, dr, dc) => {
        let i = r + dr, j = c + dc;
        while (i >= 0 && i < m && j >= 0 && j < n) {
            if (grid[i][j] === 2) break; // wall blocks
            if (grid[i][j] === 1) break; // another guard blocks
            if (grid[i][j] === 0) grid[i][j] = 3; // mark as guarded
            i += dr; j += dc;
        }
    };
    
    // for every guard, sweep four directions
    for (const [gr, gc] of guards) {
        sweep(gr, gc, -1, 0); // up
        sweep(gr, gc, 1, 0);  // down
        sweep(gr, gc, 0, -1); // left
        sweep(gr, gc, 0, 1);  // right
    }
    
    // count unoccupied (still 0)
    let count = 0;
    for (let r = 0; r < m; r++) {
        for (let c = 0; c < n; c++) {
            if (grid[r][c] === 0) count++;
        }
    }
    return count;
};

πŸ§ͺ Example Walkthrough

Input:
m = 4, n = 6,
guards = [[0,0],[1,1],[2,3]],
walls = [[0,1],[2,2],[1,4]]

Steps:

  1. Mark guards and walls.
  2. For each guard, sweep in all directions until blocked.
  3. Count remaining unguarded cells.

βœ… Output: 7

🎯 Reflection

This problem is a great exercise in grid simulation and careful handling of boundaries and obstacles. The approach is straightforward yet requires attention to correctly simulate visibility constraints.

That’s it for Day 48 of my LeetCode journey πŸ’ͺ See you tomorrow for Day 49 β€” let’s keep pushing consistency and logic-driven problem solving! πŸ”₯

Happy Coding πŸ‘¨β€πŸ’»