|
| 1 | +<h2><a href="https://leetcode.com/problems/count-unguarded-cells-in-the-grid/">2257. Count Unguarded Cells in the Grid</a></h2><h3>Medium</h3><hr><div><p>You are given two integers <code>m</code> and <code>n</code> representing a <strong>0-indexed</strong> <code>m x n</code> grid. You are also given two 2D integer arrays <code>guards</code> and <code>walls</code> where <code>guards[i] = [row<sub>i</sub>, col<sub>i</sub>]</code> and <code>walls[j] = [row<sub>j</sub>, col<sub>j</sub>]</code> represent the positions of the <code>i<sup>th</sup></code> guard and <code>j<sup>th</sup></code> wall respectively.</p> |
| 2 | + |
| 3 | +<p>A guard can see <b>every</b> cell in the four cardinal directions (north, east, south, or west) starting from their position unless <strong>obstructed</strong> by a wall or another guard. A cell is <strong>guarded</strong> if there is <strong>at least</strong> one guard that can see it.</p> |
| 4 | + |
| 5 | +<p>Return<em> the number of unoccupied cells that are <strong>not</strong> <strong>guarded</strong>.</em></p> |
| 6 | + |
| 7 | +<p> </p> |
| 8 | +<p><strong class="example">Example 1:</strong></p> |
| 9 | +<img alt="" src="https://assets.leetcode.com/uploads/2022/03/10/example1drawio2.png" style="width: 300px; height: 204px;"> |
| 10 | +<pre><strong>Input:</strong> m = 4, n = 6, guards = [[0,0],[1,1],[2,3]], walls = [[0,1],[2,2],[1,4]] |
| 11 | +<strong>Output:</strong> 7 |
| 12 | +<strong>Explanation:</strong> The guarded and unguarded cells are shown in red and green respectively in the above diagram. |
| 13 | +There are a total of 7 unguarded cells, so we return 7. |
| 14 | +</pre> |
| 15 | + |
| 16 | +<p><strong class="example">Example 2:</strong></p> |
| 17 | +<img alt="" src="https://assets.leetcode.com/uploads/2022/03/10/example2drawio.png" style="width: 200px; height: 201px;"> |
| 18 | +<pre><strong>Input:</strong> m = 3, n = 3, guards = [[1,1]], walls = [[0,1],[1,0],[2,1],[1,2]] |
| 19 | +<strong>Output:</strong> 4 |
| 20 | +<strong>Explanation:</strong> The unguarded cells are shown in green in the above diagram. |
| 21 | +There are a total of 4 unguarded cells, so we return 4. |
| 22 | +</pre> |
| 23 | + |
| 24 | +<p> </p> |
| 25 | +<p><strong>Constraints:</strong></p> |
| 26 | + |
| 27 | +<ul> |
| 28 | + <li><code>1 <= m, n <= 10<sup>5</sup></code></li> |
| 29 | + <li><code>2 <= m * n <= 10<sup>5</sup></code></li> |
| 30 | + <li><code>1 <= guards.length, walls.length <= 5 * 10<sup>4</sup></code></li> |
| 31 | + <li><code>2 <= guards.length + walls.length <= m * n</code></li> |
| 32 | + <li><code>guards[i].length == walls[j].length == 2</code></li> |
| 33 | + <li><code>0 <= row<sub>i</sub>, row<sub>j</sub> < m</code></li> |
| 34 | + <li><code>0 <= col<sub>i</sub>, col<sub>j</sub> < n</code></li> |
| 35 | + <li>All the positions in <code>guards</code> and <code>walls</code> are <strong>unique</strong>.</li> |
| 36 | +</ul> |
| 37 | +</div> |
0 commit comments