Skip to content

Commit c75bdf1

Browse files
authored
feat: add solutions to lc problem: No.3531 (#4387)
No.3531.Count Covered Buildings
1 parent 9877046 commit c75bdf1

File tree

7 files changed

+464
-8
lines changed

7 files changed

+464
-8
lines changed

solution/3500-3599/3531.Count Covered Buildings/README.md

+159-4
Original file line numberDiff line numberDiff line change
@@ -112,32 +112,187 @@ tags:
112112

113113
<!-- solution:start -->
114114

115-
### 方法一
115+
### 方法一:哈希表 + 排序
116+
117+
我们可以将建筑按照横坐标和纵坐标进行分组,分别记录在哈希表 $\text{g1}$ 和 $\text{g2}$ 中,其中 $\text{g1[x]}$ 表示所有横坐标为 $x$ 的纵坐标,而 $\text{g2[y]}$ 表示所有纵坐标为 $y$ 的横坐标,然后我们将其进行排序。
118+
119+
接下来,我们遍历所有建筑,对于当前建筑 $(x, y)$,我们通过哈希表获取对应的纵坐标列表 $l_1$ 和横坐标列表 $l_2$,并检查条件以确定建筑是否被覆盖。覆盖的条件是 $l_2[0] < x < l_2[-1]$ 且 $l_1[0] < y < l_1[-1]$,若是,我们将答案加一。
120+
121+
遍历结束后,返回答案即可。
122+
123+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是建筑物的数量。
116124

117125
<!-- tabs:start -->
118126

119127
#### Python3
120128

121129
```python
122-
130+
class Solution:
131+
def countCoveredBuildings(self, n: int, buildings: List[List[int]]) -> int:
132+
g1 = defaultdict(list)
133+
g2 = defaultdict(list)
134+
for x, y in buildings:
135+
g1[x].append(y)
136+
g2[y].append(x)
137+
for x in g1:
138+
g1[x].sort()
139+
for y in g2:
140+
g2[y].sort()
141+
ans = 0
142+
for x, y in buildings:
143+
l1 = g1[x]
144+
l2 = g2[y]
145+
if l2[0] < x < l2[-1] and l1[0] < y < l1[-1]:
146+
ans += 1
147+
return ans
123148
```
124149

125150
#### Java
126151

127152
```java
128-
153+
class Solution {
154+
public int countCoveredBuildings(int n, int[][] buildings) {
155+
Map<Integer, List<Integer>> g1 = new HashMap<>();
156+
Map<Integer, List<Integer>> g2 = new HashMap<>();
157+
158+
for (int[] building : buildings) {
159+
int x = building[0], y = building[1];
160+
g1.computeIfAbsent(x, k -> new ArrayList<>()).add(y);
161+
g2.computeIfAbsent(y, k -> new ArrayList<>()).add(x);
162+
}
163+
164+
for (var e : g1.entrySet()) {
165+
Collections.sort(e.getValue());
166+
}
167+
for (var e : g2.entrySet()) {
168+
Collections.sort(e.getValue());
169+
}
170+
171+
int ans = 0;
172+
173+
for (int[] building : buildings) {
174+
int x = building[0], y = building[1];
175+
List<Integer> l1 = g1.get(x);
176+
List<Integer> l2 = g2.get(y);
177+
178+
if (l2.get(0) < x && x < l2.get(l2.size() - 1) && l1.get(0) < y
179+
&& y < l1.get(l1.size() - 1)) {
180+
ans++;
181+
}
182+
}
183+
184+
return ans;
185+
}
186+
}
129187
```
130188

131189
#### C++
132190

133191
```cpp
134-
192+
class Solution {
193+
public:
194+
int countCoveredBuildings(int n, vector<vector<int>>& buildings) {
195+
unordered_map<int, vector<int>> g1;
196+
unordered_map<int, vector<int>> g2;
197+
198+
for (const auto& building : buildings) {
199+
int x = building[0], y = building[1];
200+
g1[x].push_back(y);
201+
g2[y].push_back(x);
202+
}
203+
204+
for (auto& e : g1) {
205+
sort(e.second.begin(), e.second.end());
206+
}
207+
for (auto& e : g2) {
208+
sort(e.second.begin(), e.second.end());
209+
}
210+
211+
int ans = 0;
212+
213+
for (const auto& building : buildings) {
214+
int x = building[0], y = building[1];
215+
const vector<int>& l1 = g1[x];
216+
const vector<int>& l2 = g2[y];
217+
218+
if (l2[0] < x && x < l2[l2.size() - 1] && l1[0] < y && y < l1[l1.size() - 1]) {
219+
ans++;
220+
}
221+
}
222+
223+
return ans;
224+
}
225+
};
135226
```
136227

137228
#### Go
138229

139230
```go
231+
func countCoveredBuildings(n int, buildings [][]int) (ans int) {
232+
g1 := make(map[int][]int)
233+
g2 := make(map[int][]int)
234+
235+
for _, building := range buildings {
236+
x, y := building[0], building[1]
237+
g1[x] = append(g1[x], y)
238+
g2[y] = append(g2[y], x)
239+
}
240+
241+
for _, list := range g1 {
242+
sort.Ints(list)
243+
}
244+
for _, list := range g2 {
245+
sort.Ints(list)
246+
}
247+
248+
for _, building := range buildings {
249+
x, y := building[0], building[1]
250+
l1 := g1[x]
251+
l2 := g2[y]
252+
253+
if l2[0] < x && x < l2[len(l2)-1] && l1[0] < y && y < l1[len(l1)-1] {
254+
ans++
255+
}
256+
}
257+
return
258+
}
259+
```
260+
261+
#### TypeScript
262+
263+
```ts
264+
function countCoveredBuildings(n: number, buildings: number[][]): number {
265+
const g1: Map<number, number[]> = new Map();
266+
const g2: Map<number, number[]> = new Map();
267+
268+
for (const [x, y] of buildings) {
269+
if (!g1.has(x)) g1.set(x, []);
270+
g1.get(x)?.push(y);
271+
272+
if (!g2.has(y)) g2.set(y, []);
273+
g2.get(y)?.push(x);
274+
}
275+
276+
for (const list of g1.values()) {
277+
list.sort((a, b) => a - b);
278+
}
279+
for (const list of g2.values()) {
280+
list.sort((a, b) => a - b);
281+
}
282+
283+
let ans = 0;
284+
285+
for (const [x, y] of buildings) {
286+
const l1 = g1.get(x)!;
287+
const l2 = g2.get(y)!;
288+
289+
if (l2[0] < x && x < l2[l2.length - 1] && l1[0] < y && y < l1[l1.length - 1]) {
290+
ans++;
291+
}
292+
}
140293

294+
return ans;
295+
}
141296
```
142297

143298
<!-- tabs:end -->

solution/3500-3599/3531.Count Covered Buildings/README_EN.md

+159-4
Original file line numberDiff line numberDiff line change
@@ -110,32 +110,187 @@ tags:
110110

111111
<!-- solution:start -->
112112

113-
### Solution 1
113+
### Solution 1: Hash Table + Sorting
114+
115+
We can group the buildings by their x-coordinates and y-coordinates, storing them in hash tables $\text{g1}$ and $\text{g2}$, respectively. Here, $\text{g1[x]}$ represents all y-coordinates for buildings with x-coordinate $x$, and $\text{g2[y]}$ represents all x-coordinates for buildings with y-coordinate $y$. Then, we sort these lists.
116+
117+
Next, we iterate through all buildings. For the current building $(x, y)$, we retrieve the corresponding y-coordinate list $l_1$ from $\text{g1}$ and the x-coordinate list $l_2$ from $\text{g2}$. We check the conditions to determine whether the building is covered. A building is covered if $l_2[0] < x < l_2[-1]$ and $l_1[0] < y < l_1[-1]$. If so, we increment the answer by one.
118+
119+
After finishing the iteration, we return the final answer.
120+
121+
The complexity is $O(n \times \log n)$, and the space complexity is $O(n)$, where $n$ is the number of buildings.
114122

115123
<!-- tabs:start -->
116124

117125
#### Python3
118126

119127
```python
120-
128+
class Solution:
129+
def countCoveredBuildings(self, n: int, buildings: List[List[int]]) -> int:
130+
g1 = defaultdict(list)
131+
g2 = defaultdict(list)
132+
for x, y in buildings:
133+
g1[x].append(y)
134+
g2[y].append(x)
135+
for x in g1:
136+
g1[x].sort()
137+
for y in g2:
138+
g2[y].sort()
139+
ans = 0
140+
for x, y in buildings:
141+
l1 = g1[x]
142+
l2 = g2[y]
143+
if l2[0] < x < l2[-1] and l1[0] < y < l1[-1]:
144+
ans += 1
145+
return ans
121146
```
122147

123148
#### Java
124149

125150
```java
126-
151+
class Solution {
152+
public int countCoveredBuildings(int n, int[][] buildings) {
153+
Map<Integer, List<Integer>> g1 = new HashMap<>();
154+
Map<Integer, List<Integer>> g2 = new HashMap<>();
155+
156+
for (int[] building : buildings) {
157+
int x = building[0], y = building[1];
158+
g1.computeIfAbsent(x, k -> new ArrayList<>()).add(y);
159+
g2.computeIfAbsent(y, k -> new ArrayList<>()).add(x);
160+
}
161+
162+
for (var e : g1.entrySet()) {
163+
Collections.sort(e.getValue());
164+
}
165+
for (var e : g2.entrySet()) {
166+
Collections.sort(e.getValue());
167+
}
168+
169+
int ans = 0;
170+
171+
for (int[] building : buildings) {
172+
int x = building[0], y = building[1];
173+
List<Integer> l1 = g1.get(x);
174+
List<Integer> l2 = g2.get(y);
175+
176+
if (l2.get(0) < x && x < l2.get(l2.size() - 1) && l1.get(0) < y
177+
&& y < l1.get(l1.size() - 1)) {
178+
ans++;
179+
}
180+
}
181+
182+
return ans;
183+
}
184+
}
127185
```
128186

129187
#### C++
130188

131189
```cpp
132-
190+
class Solution {
191+
public:
192+
int countCoveredBuildings(int n, vector<vector<int>>& buildings) {
193+
unordered_map<int, vector<int>> g1;
194+
unordered_map<int, vector<int>> g2;
195+
196+
for (const auto& building : buildings) {
197+
int x = building[0], y = building[1];
198+
g1[x].push_back(y);
199+
g2[y].push_back(x);
200+
}
201+
202+
for (auto& e : g1) {
203+
sort(e.second.begin(), e.second.end());
204+
}
205+
for (auto& e : g2) {
206+
sort(e.second.begin(), e.second.end());
207+
}
208+
209+
int ans = 0;
210+
211+
for (const auto& building : buildings) {
212+
int x = building[0], y = building[1];
213+
const vector<int>& l1 = g1[x];
214+
const vector<int>& l2 = g2[y];
215+
216+
if (l2[0] < x && x < l2[l2.size() - 1] && l1[0] < y && y < l1[l1.size() - 1]) {
217+
ans++;
218+
}
219+
}
220+
221+
return ans;
222+
}
223+
};
133224
```
134225

135226
#### Go
136227

137228
```go
229+
func countCoveredBuildings(n int, buildings [][]int) (ans int) {
230+
g1 := make(map[int][]int)
231+
g2 := make(map[int][]int)
232+
233+
for _, building := range buildings {
234+
x, y := building[0], building[1]
235+
g1[x] = append(g1[x], y)
236+
g2[y] = append(g2[y], x)
237+
}
238+
239+
for _, list := range g1 {
240+
sort.Ints(list)
241+
}
242+
for _, list := range g2 {
243+
sort.Ints(list)
244+
}
245+
246+
for _, building := range buildings {
247+
x, y := building[0], building[1]
248+
l1 := g1[x]
249+
l2 := g2[y]
250+
251+
if l2[0] < x && x < l2[len(l2)-1] && l1[0] < y && y < l1[len(l1)-1] {
252+
ans++
253+
}
254+
}
255+
return
256+
}
257+
```
258+
259+
#### TypeScript
260+
261+
```ts
262+
function countCoveredBuildings(n: number, buildings: number[][]): number {
263+
const g1: Map<number, number[]> = new Map();
264+
const g2: Map<number, number[]> = new Map();
265+
266+
for (const [x, y] of buildings) {
267+
if (!g1.has(x)) g1.set(x, []);
268+
g1.get(x)?.push(y);
269+
270+
if (!g2.has(y)) g2.set(y, []);
271+
g2.get(y)?.push(x);
272+
}
273+
274+
for (const list of g1.values()) {
275+
list.sort((a, b) => a - b);
276+
}
277+
for (const list of g2.values()) {
278+
list.sort((a, b) => a - b);
279+
}
280+
281+
let ans = 0;
282+
283+
for (const [x, y] of buildings) {
284+
const l1 = g1.get(x)!;
285+
const l2 = g2.get(y)!;
286+
287+
if (l2[0] < x && x < l2[l2.length - 1] && l1[0] < y && y < l1[l1.length - 1]) {
288+
ans++;
289+
}
290+
}
138291

292+
return ans;
293+
}
139294
```
140295

141296
<!-- tabs:end -->

0 commit comments

Comments
 (0)