Tap here to switch tabs
Problem
Submissions

Problem: Count Unguarded Cells in the Grid

med
30 min
Try to solve the Count Unguarded Cells in the Grid problem.

Statement

You are given two integers m and n representing a 0-indexed grid of size m ×\times n. You are also given two 2D integer arrays, guards and walls, where guards[i] = [row_i, col_i] indicates the position of the ithi^{\text{th}} guard and walls[j] = [row_j, col_j] indicates the position of the jthj^{\text{th}} wall.

A guard can observe every cell along the four cardinal directions (north, south, east, and west) starting from their position, unless their line of sight is blocked by a wall or another guard. A cell is guarded if it can be seen by at least one guard.

Return the total number of unoccupied cells that are not guarded.

Note: A cell is occupied if it contains a guard or a wall. Occupied cells are not counted as guarded.

Constraints:

  • 1≤1 \leq m, n ≤105\leq 10^5

  • 2≤2 \leq m ×\times n ≤105\leq 10^5

  • 1≤1 \leq guards.length, walls.length ≤5×104\leq 5 \times 10^4

  • 2≤2 \leq guards.length ++ walls.length ≤\leq m ×\times n

  • guards[i].length ==== walls[j].length ==2== 2

  • 0≤rowi,rowj<0 \leq \text{row}_i, \text{row}_j < m

  • 0≤coli,colj<0 \leq \text{col}_i, \text{col}_j < n

  • All the positions in guards and walls are unique.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Count Unguarded Cells in the Grid

med
30 min
Try to solve the Count Unguarded Cells in the Grid problem.

Statement

You are given two integers m and n representing a 0-indexed grid of size m ×\times n. You are also given two 2D integer arrays, guards and walls, where guards[i] = [row_i, col_i] indicates the position of the ithi^{\text{th}} guard and walls[j] = [row_j, col_j] indicates the position of the jthj^{\text{th}} wall.

A guard can observe every cell along the four cardinal directions (north, south, east, and west) starting from their position, unless their line of sight is blocked by a wall or another guard. A cell is guarded if it can be seen by at least one guard.

Return the total number of unoccupied cells that are not guarded.

Note: A cell is occupied if it contains a guard or a wall. Occupied cells are not counted as guarded.

Constraints:

  • 1≤1 \leq m, n ≤105\leq 10^5

  • 2≤2 \leq m ×\times n ≤105\leq 10^5

  • 1≤1 \leq guards.length, walls.length ≤5×104\leq 5 \times 10^4

  • 2≤2 \leq guards.length ++ walls.length ≤\leq m ×\times n

  • guards[i].length ==== walls[j].length ==2== 2

  • 0≤rowi,rowj<0 \leq \text{row}_i, \text{row}_j < m

  • 0≤coli,colj<0 \leq \text{col}_i, \text{col}_j < n

  • All the positions in guards and walls are unique.