Count Covered Buildings - Problem
You are given a positive integer n, representing an n x n city. You are also given a 2D grid buildings, where buildings[i] = [x, y] denotes a unique building located at coordinates [x, y].
A building is covered if there is at least one building in all four directions: left, right, above, and below.
Return the number of covered buildings.
Input & Output
Example 1 — Grid with One Covered Building
$
Input:
n = 4, buildings = [[2,2],[1,2],[3,2],[2,1],[2,3]]
›
Output:
1
💡 Note:
Building at (2,2) is covered: left (2,1), right (2,3), above (1,2), below (3,2). All other buildings lack coverage in at least one direction.
Example 2 — No Covered Buildings
$
Input:
n = 3, buildings = [[1,1],[1,2],[2,1]]
›
Output:
0
💡 Note:
No building has coverage in all four directions. Each building is missing at least one directional neighbor.
Example 3 — Multiple Covered Buildings
$
Input:
n = 5, buildings = [[2,2],[2,3],[1,2],[3,2],[2,1],[2,4],[1,3],[3,3]]
›
Output:
2
💡 Note:
Buildings (2,2) and (2,3) are both covered, each having neighbors in all four cardinal directions.
Constraints
- 1 ≤ n ≤ 103
- 1 ≤ buildings.length ≤ 5 × 102
- buildings[i] = [xi, yi]
- 1 ≤ xi, yi ≤ n
- All buildings are at unique locations
Visualization
Tap to expand
Understanding the Visualization
1
Input
Grid size n and building coordinates
2
Process
Check each building for neighbors in all four cardinal directions
3
Output
Count of buildings with complete coverage
Key Takeaway
🎯 Key Insight: A building is covered only when it has at least one neighbor in each cardinal direction (left, right, above, below)
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code