Skip to content

Commit 094181a

Browse files
committed
Add solution and test-cases for problem 3531
1 parent 7ce34f6 commit 094181a

File tree

6 files changed

+101
-25
lines changed

6 files changed

+101
-25
lines changed
7.67 KB
Loading
7.26 KB
Loading
16.7 KB
Loading

leetcode/3501-3600/3531.Count-Covered-Buildings/README.md

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,63 @@
11
# [3531.Count Covered Buildings][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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]`.
5+
6+
A building is **covered** if there is at least one building in all **four** directions: left, right, above, and below.
7+
8+
Return the number of **covered** buildings.
9+
10+
**Example 1:**
11+
12+
![1](./1.jpg)
13+
14+
```
15+
Input: n = 3, buildings = [[1,2],[2,2],[3,2],[2,1],[2,3]]
16+
17+
Output: 1
18+
19+
Explanation:
720
8-
**Example 1:**
21+
Only building [2,2] is covered as it has at least one building:
22+
above ([1,2])
23+
below ([3,2])
24+
left ([2,1])
25+
right ([2,3])
26+
Thus, the count of covered buildings is 1.
27+
```
28+
29+
**Example 2:**
30+
31+
![2](./2.jpg)
932

1033
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
34+
Input: n = 3, buildings = [[1,1],[1,2],[2,1],[2,2]]
35+
36+
Output: 0
37+
38+
Explanation:
39+
40+
No building has at least one building in all four directions.
1341
```
1442

15-
## 题意
16-
> ...
43+
**Example 3:**
1744

18-
## 题解
45+
![3](./3.jpg)
1946

20-
### 思路1
21-
> ...
22-
Count Covered Buildings
23-
```go
2447
```
48+
Input: n = 5, buildings = [[1,3],[3,2],[3,3],[3,5],[5,3]]
49+
50+
Output: 1
2551
52+
Explanation:
53+
54+
Only building [3,3] is covered as it has at least one building:
55+
above ([1,3])
56+
below ([5,3])
57+
left ([3,2])
58+
right ([3,5])
59+
Thus, the count of covered buildings is 1.
60+
```
2661

2762
## 结语
2863

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,45 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(n int, buildings [][]int) int {
6+
var ret int
7+
l := len(buildings)
8+
byY := make([][]int, l)
9+
byX := make([][]int, l)
10+
copy(byY, buildings)
11+
copy(byX, buildings)
12+
sort.Slice(byY, func(i, j int) bool {
13+
return byY[i][1] < byY[j][1]
14+
})
15+
sort.Slice(byX, func(i, j int) bool {
16+
return byX[i][0] < byX[j][0]
17+
})
18+
19+
yLoc := make(map[int][]int)
20+
xLoc := make(map[int][]int)
21+
for _, build := range byY {
22+
xLoc[build[0]] = append(xLoc[build[0]], build[1])
23+
}
24+
for _, build := range byX {
25+
yLoc[build[1]] = append(yLoc[build[1]], build[0])
26+
}
27+
28+
for _, build := range buildings {
29+
x, y := build[0], build[1]
30+
index := sort.Search(len(xLoc[x]), func(i int) bool {
31+
return xLoc[x][i] >= y
32+
})
33+
if index > 0 && index < len(xLoc[x])-1 {
34+
j := sort.Search(len(yLoc[y]), func(ii int) bool {
35+
return yLoc[y][ii] >= x
36+
})
37+
if j > 0 && j < len(yLoc[y])-1 {
38+
ret++
39+
}
40+
}
41+
42+
}
43+
44+
return ret
545
}

leetcode/3501-3600/3531.Count-Covered-Buildings/Solution_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
n int
14+
inputs [][]int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", 3, [][]int{{1, 2}, {2, 2}, {3, 2}, {2, 1}, {2, 3}}, 1},
18+
{"TestCase2", 3, [][]int{{1, 1}, {1, 2}, {2, 1}, {2, 2}}, 0},
19+
{"TestCase3", 5, [][]int{{1, 3}, {3, 2}, {3, 3}, {3, 5}, {5, 3}}, 1},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.n, c.inputs)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.n, c.inputs)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)