-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path3531-count-covered-buildings.js
More file actions
52 lines (48 loc) · 1.48 KB
/
3531-count-covered-buildings.js
File metadata and controls
52 lines (48 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* 3531. Count Covered Buildings
* https://leetcode.com/problems/count-covered-buildings/
* Difficulty: Medium
*
* 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.
*/
/**
* @param {number} n
* @param {number[][]} buildings
* @return {number}
*/
var countCoveredBuildings = function(n, buildings) {
const rowBuildings = new Map();
const colBuildings = new Map();
let result = 0;
for (const [x, y] of buildings) {
if (!rowBuildings.has(x)) rowBuildings.set(x, []);
if (!colBuildings.has(y)) colBuildings.set(y, []);
rowBuildings.get(x).push(y);
colBuildings.get(y).push(x);
}
for (const coords of rowBuildings.values()) {
coords.sort((a, b) => a - b);
}
for (const coords of colBuildings.values()) {
coords.sort((a, b) => a - b);
}
for (const [x, y] of buildings) {
const rowCoords = rowBuildings.get(x);
const colCoords = colBuildings.get(y);
const hasLeft = rowCoords[0] < y;
const hasRight = rowCoords[rowCoords.length - 1] > y;
const hasAbove = colCoords[0] < x;
const hasBelow = colCoords[colCoords.length - 1] > x;
if (hasLeft && hasRight && hasAbove && hasBelow) {
result++;
}
}
return result;
};