diff --git a/solution/3500-3599/3531.Count Covered Buildings/README.md b/solution/3500-3599/3531.Count Covered Buildings/README.md index f61350dee5943..c8a18f6579db9 100644 --- a/solution/3500-3599/3531.Count Covered Buildings/README.md +++ b/solution/3500-3599/3531.Count Covered Buildings/README.md @@ -112,32 +112,187 @@ tags: -### 方法一 +### 方法一:哈希表 + 排序 + +我们可以将建筑按照横坐标和纵坐标进行分组,分别记录在哈希表 $\text{g1}$ 和 $\text{g2}$ 中,其中 $\text{g1[x]}$ 表示所有横坐标为 $x$ 的纵坐标,而 $\text{g2[y]}$ 表示所有纵坐标为 $y$ 的横坐标,然后我们将其进行排序。 + +接下来,我们遍历所有建筑,对于当前建筑 $(x, y)$,我们通过哈希表获取对应的纵坐标列表 $l_1$ 和横坐标列表 $l_2$,并检查条件以确定建筑是否被覆盖。覆盖的条件是 $l_2[0] < x < l_2[-1]$ 且 $l_1[0] < y < l_1[-1]$,若是,我们将答案加一。 + +遍历结束后,返回答案即可。 + +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是建筑物的数量。 #### Python3 ```python - +class Solution: + def countCoveredBuildings(self, n: int, buildings: List[List[int]]) -> int: + g1 = defaultdict(list) + g2 = defaultdict(list) + for x, y in buildings: + g1[x].append(y) + g2[y].append(x) + for x in g1: + g1[x].sort() + for y in g2: + g2[y].sort() + ans = 0 + for x, y in buildings: + l1 = g1[x] + l2 = g2[y] + if l2[0] < x < l2[-1] and l1[0] < y < l1[-1]: + ans += 1 + return ans ``` #### Java ```java - +class Solution { + public int countCoveredBuildings(int n, int[][] buildings) { + Map> g1 = new HashMap<>(); + Map> g2 = new HashMap<>(); + + for (int[] building : buildings) { + int x = building[0], y = building[1]; + g1.computeIfAbsent(x, k -> new ArrayList<>()).add(y); + g2.computeIfAbsent(y, k -> new ArrayList<>()).add(x); + } + + for (var e : g1.entrySet()) { + Collections.sort(e.getValue()); + } + for (var e : g2.entrySet()) { + Collections.sort(e.getValue()); + } + + int ans = 0; + + for (int[] building : buildings) { + int x = building[0], y = building[1]; + List l1 = g1.get(x); + List l2 = g2.get(y); + + if (l2.get(0) < x && x < l2.get(l2.size() - 1) && l1.get(0) < y + && y < l1.get(l1.size() - 1)) { + ans++; + } + } + + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int countCoveredBuildings(int n, vector>& buildings) { + unordered_map> g1; + unordered_map> g2; + + for (const auto& building : buildings) { + int x = building[0], y = building[1]; + g1[x].push_back(y); + g2[y].push_back(x); + } + + for (auto& e : g1) { + sort(e.second.begin(), e.second.end()); + } + for (auto& e : g2) { + sort(e.second.begin(), e.second.end()); + } + + int ans = 0; + + for (const auto& building : buildings) { + int x = building[0], y = building[1]; + const vector& l1 = g1[x]; + const vector& l2 = g2[y]; + + if (l2[0] < x && x < l2[l2.size() - 1] && l1[0] < y && y < l1[l1.size() - 1]) { + ans++; + } + } + + return ans; + } +}; ``` #### Go ```go +func countCoveredBuildings(n int, buildings [][]int) (ans int) { + g1 := make(map[int][]int) + g2 := make(map[int][]int) + + for _, building := range buildings { + x, y := building[0], building[1] + g1[x] = append(g1[x], y) + g2[y] = append(g2[y], x) + } + + for _, list := range g1 { + sort.Ints(list) + } + for _, list := range g2 { + sort.Ints(list) + } + + for _, building := range buildings { + x, y := building[0], building[1] + l1 := g1[x] + l2 := g2[y] + + if l2[0] < x && x < l2[len(l2)-1] && l1[0] < y && y < l1[len(l1)-1] { + ans++ + } + } + return +} +``` + +#### TypeScript + +```ts +function countCoveredBuildings(n: number, buildings: number[][]): number { + const g1: Map = new Map(); + const g2: Map = new Map(); + + for (const [x, y] of buildings) { + if (!g1.has(x)) g1.set(x, []); + g1.get(x)?.push(y); + + if (!g2.has(y)) g2.set(y, []); + g2.get(y)?.push(x); + } + + for (const list of g1.values()) { + list.sort((a, b) => a - b); + } + for (const list of g2.values()) { + list.sort((a, b) => a - b); + } + + let ans = 0; + + for (const [x, y] of buildings) { + const l1 = g1.get(x)!; + const l2 = g2.get(y)!; + + if (l2[0] < x && x < l2[l2.length - 1] && l1[0] < y && y < l1[l1.length - 1]) { + ans++; + } + } + return ans; +} ``` diff --git a/solution/3500-3599/3531.Count Covered Buildings/README_EN.md b/solution/3500-3599/3531.Count Covered Buildings/README_EN.md index a33a2803db0a3..bc11bb6c5b0a0 100644 --- a/solution/3500-3599/3531.Count Covered Buildings/README_EN.md +++ b/solution/3500-3599/3531.Count Covered Buildings/README_EN.md @@ -110,32 +110,187 @@ tags: -### Solution 1 +### Solution 1: Hash Table + Sorting + +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. + +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. + +After finishing the iteration, we return the final answer. + +The complexity is $O(n \times \log n)$, and the space complexity is $O(n)$, where $n$ is the number of buildings. #### Python3 ```python - +class Solution: + def countCoveredBuildings(self, n: int, buildings: List[List[int]]) -> int: + g1 = defaultdict(list) + g2 = defaultdict(list) + for x, y in buildings: + g1[x].append(y) + g2[y].append(x) + for x in g1: + g1[x].sort() + for y in g2: + g2[y].sort() + ans = 0 + for x, y in buildings: + l1 = g1[x] + l2 = g2[y] + if l2[0] < x < l2[-1] and l1[0] < y < l1[-1]: + ans += 1 + return ans ``` #### Java ```java - +class Solution { + public int countCoveredBuildings(int n, int[][] buildings) { + Map> g1 = new HashMap<>(); + Map> g2 = new HashMap<>(); + + for (int[] building : buildings) { + int x = building[0], y = building[1]; + g1.computeIfAbsent(x, k -> new ArrayList<>()).add(y); + g2.computeIfAbsent(y, k -> new ArrayList<>()).add(x); + } + + for (var e : g1.entrySet()) { + Collections.sort(e.getValue()); + } + for (var e : g2.entrySet()) { + Collections.sort(e.getValue()); + } + + int ans = 0; + + for (int[] building : buildings) { + int x = building[0], y = building[1]; + List l1 = g1.get(x); + List l2 = g2.get(y); + + if (l2.get(0) < x && x < l2.get(l2.size() - 1) && l1.get(0) < y + && y < l1.get(l1.size() - 1)) { + ans++; + } + } + + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int countCoveredBuildings(int n, vector>& buildings) { + unordered_map> g1; + unordered_map> g2; + + for (const auto& building : buildings) { + int x = building[0], y = building[1]; + g1[x].push_back(y); + g2[y].push_back(x); + } + + for (auto& e : g1) { + sort(e.second.begin(), e.second.end()); + } + for (auto& e : g2) { + sort(e.second.begin(), e.second.end()); + } + + int ans = 0; + + for (const auto& building : buildings) { + int x = building[0], y = building[1]; + const vector& l1 = g1[x]; + const vector& l2 = g2[y]; + + if (l2[0] < x && x < l2[l2.size() - 1] && l1[0] < y && y < l1[l1.size() - 1]) { + ans++; + } + } + + return ans; + } +}; ``` #### Go ```go +func countCoveredBuildings(n int, buildings [][]int) (ans int) { + g1 := make(map[int][]int) + g2 := make(map[int][]int) + + for _, building := range buildings { + x, y := building[0], building[1] + g1[x] = append(g1[x], y) + g2[y] = append(g2[y], x) + } + + for _, list := range g1 { + sort.Ints(list) + } + for _, list := range g2 { + sort.Ints(list) + } + + for _, building := range buildings { + x, y := building[0], building[1] + l1 := g1[x] + l2 := g2[y] + + if l2[0] < x && x < l2[len(l2)-1] && l1[0] < y && y < l1[len(l1)-1] { + ans++ + } + } + return +} +``` + +#### TypeScript + +```ts +function countCoveredBuildings(n: number, buildings: number[][]): number { + const g1: Map = new Map(); + const g2: Map = new Map(); + + for (const [x, y] of buildings) { + if (!g1.has(x)) g1.set(x, []); + g1.get(x)?.push(y); + + if (!g2.has(y)) g2.set(y, []); + g2.get(y)?.push(x); + } + + for (const list of g1.values()) { + list.sort((a, b) => a - b); + } + for (const list of g2.values()) { + list.sort((a, b) => a - b); + } + + let ans = 0; + + for (const [x, y] of buildings) { + const l1 = g1.get(x)!; + const l2 = g2.get(y)!; + + if (l2[0] < x && x < l2[l2.length - 1] && l1[0] < y && y < l1[l1.length - 1]) { + ans++; + } + } + return ans; +} ``` diff --git a/solution/3500-3599/3531.Count Covered Buildings/Solution.cpp b/solution/3500-3599/3531.Count Covered Buildings/Solution.cpp new file mode 100644 index 0000000000000..b0b7488c6b9c5 --- /dev/null +++ b/solution/3500-3599/3531.Count Covered Buildings/Solution.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + int countCoveredBuildings(int n, vector>& buildings) { + unordered_map> g1; + unordered_map> g2; + + for (const auto& building : buildings) { + int x = building[0], y = building[1]; + g1[x].push_back(y); + g2[y].push_back(x); + } + + for (auto& e : g1) { + sort(e.second.begin(), e.second.end()); + } + for (auto& e : g2) { + sort(e.second.begin(), e.second.end()); + } + + int ans = 0; + + for (const auto& building : buildings) { + int x = building[0], y = building[1]; + const vector& l1 = g1[x]; + const vector& l2 = g2[y]; + + if (l2[0] < x && x < l2[l2.size() - 1] && l1[0] < y && y < l1[l1.size() - 1]) { + ans++; + } + } + + return ans; + } +}; diff --git a/solution/3500-3599/3531.Count Covered Buildings/Solution.go b/solution/3500-3599/3531.Count Covered Buildings/Solution.go new file mode 100644 index 0000000000000..46d4c5d66218c --- /dev/null +++ b/solution/3500-3599/3531.Count Covered Buildings/Solution.go @@ -0,0 +1,28 @@ +func countCoveredBuildings(n int, buildings [][]int) (ans int) { + g1 := make(map[int][]int) + g2 := make(map[int][]int) + + for _, building := range buildings { + x, y := building[0], building[1] + g1[x] = append(g1[x], y) + g2[y] = append(g2[y], x) + } + + for _, list := range g1 { + sort.Ints(list) + } + for _, list := range g2 { + sort.Ints(list) + } + + for _, building := range buildings { + x, y := building[0], building[1] + l1 := g1[x] + l2 := g2[y] + + if l2[0] < x && x < l2[len(l2)-1] && l1[0] < y && y < l1[len(l1)-1] { + ans++ + } + } + return +} diff --git a/solution/3500-3599/3531.Count Covered Buildings/Solution.java b/solution/3500-3599/3531.Count Covered Buildings/Solution.java new file mode 100644 index 0000000000000..bf137e8913736 --- /dev/null +++ b/solution/3500-3599/3531.Count Covered Buildings/Solution.java @@ -0,0 +1,34 @@ +class Solution { + public int countCoveredBuildings(int n, int[][] buildings) { + Map> g1 = new HashMap<>(); + Map> g2 = new HashMap<>(); + + for (int[] building : buildings) { + int x = building[0], y = building[1]; + g1.computeIfAbsent(x, k -> new ArrayList<>()).add(y); + g2.computeIfAbsent(y, k -> new ArrayList<>()).add(x); + } + + for (var e : g1.entrySet()) { + Collections.sort(e.getValue()); + } + for (var e : g2.entrySet()) { + Collections.sort(e.getValue()); + } + + int ans = 0; + + for (int[] building : buildings) { + int x = building[0], y = building[1]; + List l1 = g1.get(x); + List l2 = g2.get(y); + + if (l2.get(0) < x && x < l2.get(l2.size() - 1) && l1.get(0) < y + && y < l1.get(l1.size() - 1)) { + ans++; + } + } + + return ans; + } +} \ No newline at end of file diff --git a/solution/3500-3599/3531.Count Covered Buildings/Solution.py b/solution/3500-3599/3531.Count Covered Buildings/Solution.py new file mode 100644 index 0000000000000..7518bd0bd9a39 --- /dev/null +++ b/solution/3500-3599/3531.Count Covered Buildings/Solution.py @@ -0,0 +1,18 @@ +class Solution: + def countCoveredBuildings(self, n: int, buildings: List[List[int]]) -> int: + g1 = defaultdict(list) + g2 = defaultdict(list) + for x, y in buildings: + g1[x].append(y) + g2[y].append(x) + for x in g1: + g1[x].sort() + for y in g2: + g2[y].sort() + ans = 0 + for x, y in buildings: + l1 = g1[x] + l2 = g2[y] + if l2[0] < x < l2[-1] and l1[0] < y < l1[-1]: + ans += 1 + return ans diff --git a/solution/3500-3599/3531.Count Covered Buildings/Solution.ts b/solution/3500-3599/3531.Count Covered Buildings/Solution.ts new file mode 100644 index 0000000000000..256318b79f172 --- /dev/null +++ b/solution/3500-3599/3531.Count Covered Buildings/Solution.ts @@ -0,0 +1,32 @@ +function countCoveredBuildings(n: number, buildings: number[][]): number { + const g1: Map = new Map(); + const g2: Map = new Map(); + + for (const [x, y] of buildings) { + if (!g1.has(x)) g1.set(x, []); + g1.get(x)?.push(y); + + if (!g2.has(y)) g2.set(y, []); + g2.get(y)?.push(x); + } + + for (const list of g1.values()) { + list.sort((a, b) => a - b); + } + for (const list of g2.values()) { + list.sort((a, b) => a - b); + } + + let ans = 0; + + for (const [x, y] of buildings) { + const l1 = g1.get(x)!; + const l2 = g2.get(y)!; + + if (l2[0] < x && x < l2[l2.length - 1] && l1[0] < y && y < l1[l1.length - 1]) { + ans++; + } + } + + return ans; +}