diff --git a/leetcode/3501-3600/3531.Count-Covered-Buildings/1.jpg b/leetcode/3501-3600/3531.Count-Covered-Buildings/1.jpg new file mode 100644 index 000000000..b2ee28469 Binary files /dev/null and b/leetcode/3501-3600/3531.Count-Covered-Buildings/1.jpg differ diff --git a/leetcode/3501-3600/3531.Count-Covered-Buildings/2.jpg b/leetcode/3501-3600/3531.Count-Covered-Buildings/2.jpg new file mode 100644 index 000000000..3368d415c Binary files /dev/null and b/leetcode/3501-3600/3531.Count-Covered-Buildings/2.jpg differ diff --git a/leetcode/3501-3600/3531.Count-Covered-Buildings/3.jpg b/leetcode/3501-3600/3531.Count-Covered-Buildings/3.jpg new file mode 100644 index 000000000..a42f78320 Binary files /dev/null and b/leetcode/3501-3600/3531.Count-Covered-Buildings/3.jpg differ diff --git a/leetcode/3501-3600/3531.Count-Covered-Buildings/README.md b/leetcode/3501-3600/3531.Count-Covered-Buildings/README.md index e391cb6e2..0f40f076b 100755 --- a/leetcode/3501-3600/3531.Count-Covered-Buildings/README.md +++ b/leetcode/3501-3600/3531.Count-Covered-Buildings/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/3501-3600/3531.Count-Covered-Buildings/Solution.go b/leetcode/3501-3600/3531.Count-Covered-Buildings/Solution.go index d115ccf5e..e126334b8 100644 --- a/leetcode/3501-3600/3531.Count-Covered-Buildings/Solution.go +++ b/leetcode/3501-3600/3531.Count-Covered-Buildings/Solution.go @@ -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 } diff --git a/leetcode/3501-3600/3531.Count-Covered-Buildings/Solution_test.go b/leetcode/3501-3600/3531.Count-Covered-Buildings/Solution_test.go index 14ff50eb4..3be68758c 100644 --- a/leetcode/3501-3600/3531.Count-Covered-Buildings/Solution_test.go +++ b/leetcode/3501-3600/3531.Count-Covered-Buildings/Solution_test.go @@ -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() { }