Skip to content

Commit 570e30e

Browse files
chore: add LeetCode daily solution
1 parent a8d61b8 commit 570e30e

File tree

5 files changed

+91
-0
lines changed

5 files changed

+91
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Count Covered Buildings (Medium)
2+
3+
**Problem ID:** 3531
4+
**Date:** 2025-12-11
5+
**Link:** https://leetcode.com/problems/count-covered-buildings/
6+
7+
## Approach
8+
9+
To solve the "Count Covered Buildings" problem, we need to determine how many buildings in a given 2D grid are "covered," meaning each covered building has at least one other building in each of the four cardinal directions (up, down, left, right).
10+
11+
### Approach:
12+
13+
1. **Data Structures**:
14+
- Use a set to store the coordinates of the buildings for O(1) average-time complexity when checking if a building exists at a specific coordinate.
15+
- Additionally, maintain four sets (or dictionaries) to track the presence of buildings in each direction for every building:
16+
- `above`: stores buildings that can cover others from above.
17+
- `below`: stores buildings that can cover others from below.
18+
- `left`: stores buildings that can cover others from the left.
19+
- `right`: stores buildings that can cover others from the right.
20+
21+
2. **Processing the Buildings**:
22+
- Iterate through each building's coordinates. For each building located at `(x, y)`, check:
23+
- If there is a building at `(x-1, y)` (left).
24+
- If there is a building at `(x+1, y)` (right).
25+
- If there is a building at `(x, y-1)` (above).
26+
- If there is a building at `(x, y+1)` (below).
27+
- For each building, update the respective direction sets accordingly.
28+
29+
3. **Counting Covered Buildings**:
30+
- After processing all buildings, iterate through the original list of buildings again.
31+
- For each building, check if it has at least one building in all four direction sets. If it does, increment the count of covered buildings.
32+
33+
### Complexity:
34+
- **Time Complexity**: O(m), where m is the number of buildings, since we are iterating through the list of buildings a couple of times.
35+
- **Space Complexity**: O(m) as we store the coordinates in a set and potentially use additional sets for direction tracking.
36+
37+
This approach efficiently checks for coverage by leveraging set operations, ensuring that the solution is optimal even for the upper limits of the constraints.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
4+
class Solution {
5+
public int countCoveredBuildings(int n, int[][] buildings) {
6+
Set<String> buildingSet = new HashSet<>();
7+
for (int[] building : buildings) {
8+
buildingSet.add(building[0] + "," + building[1]);
9+
}
10+
11+
int coveredCount = 0;
12+
13+
for (int[] building : buildings) {
14+
int x = building[0];
15+
int y = building[1];
16+
if (buildingSet.contains(x - 1 + "," + y) &&
17+
buildingSet.contains(x + 1 + "," + y) &&
18+
buildingSet.contains(x + "," + (y - 1)) &&
19+
buildingSet.contains(x + "," + (y + 1))) {
20+
coveredCount++;
21+
}
22+
}
23+
24+
return coveredCount;
25+
}
26+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var countCoveredBuildings = function(n, buildings) {
2+
const buildingSet = new Set(buildings.map(b => b.join(',')));
3+
let coveredCount = 0;
4+
5+
for (const [x, y] of buildings) {
6+
if (
7+
buildingSet.has(`${x - 1},${y}`) &&
8+
buildingSet.has(`${x + 1},${y}`) &&
9+
buildingSet.has(`${x},${y - 1}`) &&
10+
buildingSet.has(`${x},${y + 1}`)
11+
) {
12+
coveredCount++;
13+
}
14+
}
15+
16+
return coveredCount;
17+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def countCoveredBuildings(self, n: int, buildings: List[List[int]]) -> int:
3+
covered = set()
4+
building_set = set(map(tuple, buildings))
5+
6+
for x, y in buildings:
7+
if (x - 1, y) in building_set and (x + 1, y) in building_set and (x, y - 1) in building_set and (x, y + 1) in building_set:
8+
covered.add((x, y))
9+
10+
return len(covered)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
301301
- 2025-12-08 — [Count Square Sum Triples](https://leetcode.com/problems/count-square-sum-triples/) (Easy) → `Easy/2025-12-08-1925-Count-Square-Sum-Triples`
302302
- 2025-12-09 — [Count Special Triplets](https://leetcode.com/problems/count-special-triplets/) (Medium) → `Medium/2025-12-09-3583-Count-Special-Triplets`
303303
- 2025-12-10 — [Count the Number of Computer Unlocking Permutations](https://leetcode.com/problems/count-the-number-of-computer-unlocking-permutations/) (Medium) → `Medium/2025-12-10-3577-Count-the-Number-of-Computer-Unlocking-Permutations`
304+
- 2025-12-11 — [Count Covered Buildings](https://leetcode.com/problems/count-covered-buildings/) (Medium) → `Medium/2025-12-11-3531-Count-Covered-Buildings`

0 commit comments

Comments
 (0)