Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 48 additions & 13 deletions leetcode/3501-3600/3531.Count-Covered-Buildings/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,63 @@
# [3531.Count Covered Buildings][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
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.

**Example 1:**

![1](./1.jpg)

```
Input: n = 3, buildings = [[1,2],[2,2],[3,2],[2,1],[2,3]]

Output: 1

Explanation:

**Example 1:**
Only building [2,2] is covered as it has at least one building:
above ([1,2])
below ([3,2])
left ([2,1])
right ([2,3])
Thus, the count of covered buildings is 1.
```

**Example 2:**

![2](./2.jpg)

```
Input: a = "11", b = "1"
Output: "100"
Input: n = 3, buildings = [[1,1],[1,2],[2,1],[2,2]]

Output: 0

Explanation:

No building has at least one building in all four directions.
```

## 题意
> ...
**Example 3:**

## 题解
![3](./3.jpg)

### 思路1
> ...
Count Covered Buildings
```go
```
Input: n = 5, buildings = [[1,3],[3,2],[3,3],[3,5],[5,3]]

Output: 1

Explanation:

Only building [3,3] is covered as it has at least one building:
above ([1,3])
below ([5,3])
left ([3,2])
right ([3,5])
Thus, the count of covered buildings is 1.
```

## 结语

Expand Down
44 changes: 42 additions & 2 deletions leetcode/3501-3600/3531.Count-Covered-Buildings/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
package Solution

func Solution(x bool) bool {
return x
import "sort"

func Solution(n int, buildings [][]int) int {
var ret int
l := len(buildings)
byY := make([][]int, l)
byX := make([][]int, l)
copy(byY, buildings)
copy(byX, buildings)
sort.Slice(byY, func(i, j int) bool {
return byY[i][1] < byY[j][1]
})
sort.Slice(byX, func(i, j int) bool {
return byX[i][0] < byX[j][0]
})

yLoc := make(map[int][]int)
xLoc := make(map[int][]int)
for _, build := range byY {
xLoc[build[0]] = append(xLoc[build[0]], build[1])
}
for _, build := range byX {
yLoc[build[1]] = append(yLoc[build[1]], build[0])
}

for _, build := range buildings {
x, y := build[0], build[1]
index := sort.Search(len(xLoc[x]), func(i int) bool {
return xLoc[x][i] >= y
})
if index > 0 && index < len(xLoc[x])-1 {
j := sort.Search(len(yLoc[y]), func(ii int) bool {
return yLoc[y][ii] >= x
})
if j > 0 && j < len(yLoc[y])-1 {
ret++
}
}

}

return ret
}
21 changes: 11 additions & 10 deletions leetcode/3501-3600/3531.Count-Covered-Buildings/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
n int
inputs [][]int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 3, [][]int{{1, 2}, {2, 2}, {3, 2}, {2, 1}, {2, 3}}, 1},
{"TestCase2", 3, [][]int{{1, 1}, {1, 2}, {2, 1}, {2, 2}}, 0},
{"TestCase3", 5, [][]int{{1, 3}, {3, 2}, {3, 3}, {3, 5}, {5, 3}}, 1},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.n, c.inputs)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.n, c.inputs)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading