|
| 1 | +<h2><a href="https://leetcode.com/problems/count-covered-buildings">3819. Count Covered Buildings</a></h2><h3>Medium</h3><hr><p>You are given a positive integer <code>n</code>, representing an <code>n x n</code> city. You are also given a 2D grid <code>buildings</code>, where <code>buildings[i] = [x, y]</code> denotes a <strong>unique</strong> building located at coordinates <code>[x, y]</code>.</p> |
| 2 | + |
| 3 | +<p>A building is <strong>covered</strong> if there is at least one building in all <strong>four</strong> directions: left, right, above, and below.</p> |
| 4 | + |
| 5 | +<p>Return the number of <strong>covered</strong> buildings.</p> |
| 6 | + |
| 7 | +<p> </p> |
| 8 | +<p><strong class="example">Example 1:</strong></p> |
| 9 | + |
| 10 | +<p><img src="https://assets.leetcode.com/uploads/2025/03/04/telegram-cloud-photo-size-5-6212982906394101085-m.jpg" style="width: 200px; height: 204px;" /></p> |
| 11 | + |
| 12 | +<div class="example-block"> |
| 13 | +<p><strong>Input:</strong> <span class="example-io">n = 3, buildings = [[1,2],[2,2],[3,2],[2,1],[2,3]]</span></p> |
| 14 | + |
| 15 | +<p><strong>Output:</strong> <span class="example-io">1</span></p> |
| 16 | + |
| 17 | +<p><strong>Explanation:</strong></p> |
| 18 | + |
| 19 | +<ul> |
| 20 | + <li>Only building <code>[2,2]</code> is covered as it has at least one building: |
| 21 | + |
| 22 | + <ul> |
| 23 | + <li>above (<code>[1,2]</code>)</li> |
| 24 | + <li>below (<code>[3,2]</code>)</li> |
| 25 | + <li>left (<code>[2,1]</code>)</li> |
| 26 | + <li>right (<code>[2,3]</code>)</li> |
| 27 | + </ul> |
| 28 | + </li> |
| 29 | + <li>Thus, the count of covered buildings is 1.</li> |
| 30 | +</ul> |
| 31 | +</div> |
| 32 | + |
| 33 | +<p><strong class="example">Example 2:</strong></p> |
| 34 | + |
| 35 | +<p><img src="https://assets.leetcode.com/uploads/2025/03/04/telegram-cloud-photo-size-5-6212982906394101086-m.jpg" style="width: 200px; height: 204px;" /></p> |
| 36 | + |
| 37 | +<div class="example-block"> |
| 38 | +<p><strong>Input:</strong> <span class="example-io">n = 3, buildings = [[1,1],[1,2],[2,1],[2,2]]</span></p> |
| 39 | + |
| 40 | +<p><strong>Output:</strong> <span class="example-io">0</span></p> |
| 41 | + |
| 42 | +<p><strong>Explanation:</strong></p> |
| 43 | + |
| 44 | +<ul> |
| 45 | + <li>No building has at least one building in all four directions.</li> |
| 46 | +</ul> |
| 47 | +</div> |
| 48 | + |
| 49 | +<p><strong class="example">Example 3:</strong></p> |
| 50 | + |
| 51 | +<p><img src="https://assets.leetcode.com/uploads/2025/03/16/telegram-cloud-photo-size-5-6248862251436067566-x.jpg" style="width: 202px; height: 205px;" /></p> |
| 52 | + |
| 53 | +<div class="example-block"> |
| 54 | +<p><strong>Input:</strong> <span class="example-io">n = 5, buildings = [[1,3],[3,2],[3,3],[3,5],[5,3]]</span></p> |
| 55 | + |
| 56 | +<p><strong>Output:</strong> <span class="example-io">1</span></p> |
| 57 | + |
| 58 | +<p><strong>Explanation:</strong></p> |
| 59 | + |
| 60 | +<ul> |
| 61 | + <li>Only building <code>[3,3]</code> is covered as it has at least one building: |
| 62 | + |
| 63 | + <ul> |
| 64 | + <li>above (<code>[1,3]</code>)</li> |
| 65 | + <li>below (<code>[5,3]</code>)</li> |
| 66 | + <li>left (<code>[3,2]</code>)</li> |
| 67 | + <li>right (<code>[3,5]</code>)</li> |
| 68 | + </ul> |
| 69 | + </li> |
| 70 | + <li>Thus, the count of covered buildings is 1.</li> |
| 71 | +</ul> |
| 72 | +</div> |
| 73 | + |
| 74 | +<p> </p> |
| 75 | +<p><strong>Constraints:</strong></p> |
| 76 | + |
| 77 | +<ul> |
| 78 | + <li><code>2 <= n <= 10<sup>5</sup></code></li> |
| 79 | + <li><code>1 <= buildings.length <= 10<sup>5</sup> </code></li> |
| 80 | + <li><code>buildings[i] = [x, y]</code></li> |
| 81 | + <li><code>1 <= x, y <= n</code></li> |
| 82 | + <li>All coordinates of <code>buildings</code> are <strong>unique</strong>.</li> |
| 83 | +</ul> |
0 commit comments