#2257

Count Unguarded Cells in the Grid

pupil · 580 · lc medium +29 · verified · 69% accepted · 1,291 likes · top 76%

Description

You are given two integers m and n representing a 0-indexed m x n grid. You are also given 2D integer arrays guards and walls where guards[i] = [rowi, coli] and walls[j] = [rowj, colj] give the grid positions of guards and walls.

Each guard can see in all four cardinal directions until blocked by a wall or another guard. A cell is guarded if at least one guard can see it.

Return the number of empty cells that are not guarded.

Example 1:

Input: m = 4, n = 6, guards = [[0,0],[1,1],[2,3]], walls = [[0,1],[2,2],[1,4]]
Output: 7
Explanation: The guarded and unguarded cells are shown in red and green respectively in the above diagram.
There are a total of 7 unguarded cells, so we return 7.

Example 2:

Input: m = 3, n = 3, guards = [[1,1]], walls = [[0,1],[1,0],[2,1],[1,2]]
Output: 4
Explanation: The unguarded cells are shown in green in the above diagram.
There are a total of 4 unguarded cells, so we return 4.

Code

1
2
3