Skip to content

Commit e2bfe8c

Browse files
committed
feat: add solutions to lc problem: No.2015
No.2015.Average Height of Buildings in Each Segment
1 parent e0fa19d commit e2bfe8c

File tree

6 files changed

+371
-2
lines changed

6 files changed

+371
-2
lines changed

solution/2000-2099/2015.Average Height of Buildings in Each Segment/README.md

Lines changed: 132 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,153 @@
7777

7878
<!-- 这里可写通用的实现逻辑 -->
7979

80+
**方法一:差分有序哈希表**
81+
82+
我们利用差分思想,使用有序哈希表 `height` 记录每个位置的高度变化,`cnt` 记录建筑物的数量变化。对有序哈希表求前缀和,即可得到每个位置的高度和建筑物数量。
83+
84+
最后遍历有序哈希表,对于每个位置,如果高度和建筑物数量都不为 0,则说明该位置有建筑物,判断此时的建筑物是否与上个建筑物的平均高度相同,如果相同,则合并,否则加入结果集。
85+
86+
时间复杂度为 $O(n\log n)$,其中 $n$ 为建筑物数量。
87+
8088
<!-- tabs:start -->
8189

8290
### **Python3**
8391

8492
<!-- 这里可写当前语言的特殊实现逻辑 -->
8593

8694
```python
87-
95+
class Solution:
96+
def averageHeightOfBuildings(self, buildings: List[List[int]]) -> List[List[int]]:
97+
height = defaultdict(int)
98+
cnt = defaultdict(int)
99+
for s, e, h in buildings:
100+
cnt[s] += 1
101+
cnt[e] -= 1
102+
height[s] += h
103+
height[e] -= h
104+
ans = []
105+
i = h = n = 0
106+
for j in sorted(cnt.keys()):
107+
if n:
108+
t = [i, j, h // n]
109+
if ans and ans[-1][1] == i and ans[-1][2] == t[-1]:
110+
ans[-1][1] = j
111+
else:
112+
ans.append(t)
113+
i = j
114+
h += height[j]
115+
n += cnt[j]
116+
return ans
88117
```
89118

90119
### **Java**
91120

92121
<!-- 这里可写当前语言的特殊实现逻辑 -->
93122

94123
```java
124+
class Solution {
125+
public int[][] averageHeightOfBuildings(int[][] buildings) {
126+
TreeMap<Integer, Integer> height = new TreeMap<>();
127+
TreeMap<Integer, Integer> cnt = new TreeMap<>();
128+
for (var v : buildings) {
129+
int s = v[0], e = v[1], h = v[2];
130+
cnt.put(s, cnt.getOrDefault(s, 0) + 1);
131+
cnt.put(e, cnt.getOrDefault(e, 0) - 1);
132+
height.put(s, height.getOrDefault(s, 0) + h);
133+
height.put(e, height.getOrDefault(e, 0) - h);
134+
}
135+
int i = 0, h = 0, n = 0;
136+
List<int[]> res = new ArrayList<>();
137+
for (int j : cnt.keySet()) {
138+
if (n > 0) {
139+
int[] t = new int[] {i, j, h / n};
140+
int k = res.size() - 1;
141+
if (k >= 0 && res.get(k)[1] == i && res.get(k)[2] == t[2]) {
142+
res.get(k)[1] = j;
143+
} else {
144+
res.add(t);
145+
}
146+
}
147+
h += height.get(j);
148+
n += cnt.get(j);
149+
i = j;
150+
}
151+
int[][] ans = new int[res.size()][3];
152+
for (i = 0; i < ans.length; ++i) {
153+
ans[i] = res.get(i);
154+
}
155+
return ans;
156+
}
157+
}
158+
```
159+
160+
### **C++**
161+
162+
```cpp
163+
class Solution {
164+
public:
165+
vector<vector<int>> averageHeightOfBuildings(vector<vector<int>>& buildings) {
166+
map<int, int> height, cnt;
167+
for (auto& v : buildings) {
168+
int s = v[0], e = v[1], h = v[2];
169+
cnt[s]++, cnt[e]--;
170+
height[s] += h, height[e] -= h;
171+
}
172+
vector<vector<int>> ans;
173+
int i = 0, h = 0, n = 0;
174+
for (auto& [j, _] : cnt) {
175+
if (n) {
176+
vector<int> t = {i, j, h / n};
177+
if (ans.size() && ans.back()[1] == i && ans.back()[2] == t[2]) {
178+
ans.back()[1] = j;
179+
} else {
180+
ans.push_back(t);
181+
}
182+
}
183+
i = j;
184+
h += height[j];
185+
n += cnt[j];
186+
}
187+
return ans;
188+
}
189+
};
190+
```
95191
192+
### **Go**
193+
194+
```go
195+
func averageHeightOfBuildings(buildings [][]int) [][]int {
196+
height := make(map[int]int)
197+
cnt := make(map[int]int)
198+
for _, v := range buildings {
199+
s, e, h := v[0], v[1], v[2]
200+
cnt[s]++
201+
cnt[e]--
202+
height[s] += h
203+
height[e] -= h
204+
}
205+
keys := make([]int, len(cnt))
206+
for k := range cnt {
207+
keys = append(keys, k)
208+
}
209+
sort.Ints(keys)
210+
i, h, n := 0, 0, 0
211+
ans := [][]int{}
212+
for _, j := range keys {
213+
if n > 0 {
214+
t := []int{i, j, h / n}
215+
if len(ans) > 0 && ans[len(ans)-1][1] == i && ans[len(ans)-1][2] == t[2] {
216+
ans[len(ans)-1][1] = j
217+
} else {
218+
ans = append(ans, t)
219+
}
220+
}
221+
i = j
222+
h += height[j]
223+
n += cnt[j]
224+
}
225+
return ans
226+
}
96227
```
97228

98229
### **...**

solution/2000-2099/2015.Average Height of Buildings in Each Segment/README_EN.md

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,136 @@ We cannot group the segments together because an empty space with no buildings s
7979
### **Python3**
8080

8181
```python
82-
82+
class Solution:
83+
def averageHeightOfBuildings(self, buildings: List[List[int]]) -> List[List[int]]:
84+
height = defaultdict(int)
85+
cnt = defaultdict(int)
86+
for s, e, h in buildings:
87+
cnt[s] += 1
88+
cnt[e] -= 1
89+
height[s] += h
90+
height[e] -= h
91+
ans = []
92+
i = h = n = 0
93+
for j in sorted(cnt.keys()):
94+
if n:
95+
t = [i, j, h // n]
96+
if ans and ans[-1][1] == i and ans[-1][2] == t[-1]:
97+
ans[-1][1] = j
98+
else:
99+
ans.append(t)
100+
i = j
101+
h += height[j]
102+
n += cnt[j]
103+
return ans
83104
```
84105

85106
### **Java**
86107

87108
```java
109+
class Solution {
110+
public int[][] averageHeightOfBuildings(int[][] buildings) {
111+
TreeMap<Integer, Integer> height = new TreeMap<>();
112+
TreeMap<Integer, Integer> cnt = new TreeMap<>();
113+
for (var v : buildings) {
114+
int s = v[0], e = v[1], h = v[2];
115+
cnt.put(s, cnt.getOrDefault(s, 0) + 1);
116+
cnt.put(e, cnt.getOrDefault(e, 0) - 1);
117+
height.put(s, height.getOrDefault(s, 0) + h);
118+
height.put(e, height.getOrDefault(e, 0) - h);
119+
}
120+
int i = 0, h = 0, n = 0;
121+
List<int[]> res = new ArrayList<>();
122+
for (int j : cnt.keySet()) {
123+
if (n > 0) {
124+
int[] t = new int[] {i, j, h / n};
125+
int k = res.size() - 1;
126+
if (k >= 0 && res.get(k)[1] == i && res.get(k)[2] == t[2]) {
127+
res.get(k)[1] = j;
128+
} else {
129+
res.add(t);
130+
}
131+
}
132+
h += height.get(j);
133+
n += cnt.get(j);
134+
i = j;
135+
}
136+
int[][] ans = new int[res.size()][3];
137+
for (i = 0; i < ans.length; ++i) {
138+
ans[i] = res.get(i);
139+
}
140+
return ans;
141+
}
142+
}
143+
```
144+
145+
### **C++**
146+
147+
```cpp
148+
class Solution {
149+
public:
150+
vector<vector<int>> averageHeightOfBuildings(vector<vector<int>>& buildings) {
151+
map<int, int> height, cnt;
152+
for (auto& v : buildings) {
153+
int s = v[0], e = v[1], h = v[2];
154+
cnt[s]++, cnt[e]--;
155+
height[s] += h, height[e] -= h;
156+
}
157+
vector<vector<int>> ans;
158+
int i = 0, h = 0, n = 0;
159+
for (auto& [j, _] : cnt) {
160+
if (n) {
161+
vector<int> t = {i, j, h / n};
162+
if (ans.size() && ans.back()[1] == i && ans.back()[2] == t[2]) {
163+
ans.back()[1] = j;
164+
} else {
165+
ans.push_back(t);
166+
}
167+
}
168+
i = j;
169+
h += height[j];
170+
n += cnt[j];
171+
}
172+
return ans;
173+
}
174+
};
175+
```
88176
177+
### **Go**
178+
179+
```go
180+
func averageHeightOfBuildings(buildings [][]int) [][]int {
181+
height := make(map[int]int)
182+
cnt := make(map[int]int)
183+
for _, v := range buildings {
184+
s, e, h := v[0], v[1], v[2]
185+
cnt[s]++
186+
cnt[e]--
187+
height[s] += h
188+
height[e] -= h
189+
}
190+
keys := make([]int, len(cnt))
191+
for k := range cnt {
192+
keys = append(keys, k)
193+
}
194+
sort.Ints(keys)
195+
i, h, n := 0, 0, 0
196+
ans := [][]int{}
197+
for _, j := range keys {
198+
if n > 0 {
199+
t := []int{i, j, h / n}
200+
if len(ans) > 0 && ans[len(ans)-1][1] == i && ans[len(ans)-1][2] == t[2] {
201+
ans[len(ans)-1][1] = j
202+
} else {
203+
ans = append(ans, t)
204+
}
205+
}
206+
i = j
207+
h += height[j]
208+
n += cnt[j]
209+
}
210+
return ans
211+
}
89212
```
90213

91214
### **...**
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> averageHeightOfBuildings(vector<vector<int>>& buildings) {
4+
map<int, int> height, cnt;
5+
for (auto& v : buildings) {
6+
int s = v[0], e = v[1], h = v[2];
7+
cnt[s]++, cnt[e]--;
8+
height[s] += h, height[e] -= h;
9+
}
10+
vector<vector<int>> ans;
11+
int i = 0, h = 0, n = 0;
12+
for (auto& [j, _] : cnt) {
13+
if (n) {
14+
vector<int> t = {i, j, h / n};
15+
if (ans.size() && ans.back()[1] == i && ans.back()[2] == t[2]) {
16+
ans.back()[1] = j;
17+
} else {
18+
ans.push_back(t);
19+
}
20+
}
21+
i = j;
22+
h += height[j];
23+
n += cnt[j];
24+
}
25+
return ans;
26+
}
27+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
func averageHeightOfBuildings(buildings [][]int) [][]int {
2+
height := make(map[int]int)
3+
cnt := make(map[int]int)
4+
for _, v := range buildings {
5+
s, e, h := v[0], v[1], v[2]
6+
cnt[s]++
7+
cnt[e]--
8+
height[s] += h
9+
height[e] -= h
10+
}
11+
keys := make([]int, len(cnt))
12+
for k := range cnt {
13+
keys = append(keys, k)
14+
}
15+
sort.Ints(keys)
16+
i, h, n := 0, 0, 0
17+
ans := [][]int{}
18+
for _, j := range keys {
19+
if n > 0 {
20+
t := []int{i, j, h / n}
21+
if len(ans) > 0 && ans[len(ans)-1][1] == i && ans[len(ans)-1][2] == t[2] {
22+
ans[len(ans)-1][1] = j
23+
} else {
24+
ans = append(ans, t)
25+
}
26+
}
27+
i = j
28+
h += height[j]
29+
n += cnt[j]
30+
}
31+
return ans
32+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public int[][] averageHeightOfBuildings(int[][] buildings) {
3+
TreeMap<Integer, Integer> height = new TreeMap<>();
4+
TreeMap<Integer, Integer> cnt = new TreeMap<>();
5+
for (var v : buildings) {
6+
int s = v[0], e = v[1], h = v[2];
7+
cnt.put(s, cnt.getOrDefault(s, 0) + 1);
8+
cnt.put(e, cnt.getOrDefault(e, 0) - 1);
9+
height.put(s, height.getOrDefault(s, 0) + h);
10+
height.put(e, height.getOrDefault(e, 0) - h);
11+
}
12+
int i = 0, h = 0, n = 0;
13+
List<int[]> res = new ArrayList<>();
14+
for (int j : cnt.keySet()) {
15+
if (n > 0) {
16+
int[] t = new int[] {i, j, h / n};
17+
int k = res.size() - 1;
18+
if (k >= 0 && res.get(k)[1] == i && res.get(k)[2] == t[2]) {
19+
res.get(k)[1] = j;
20+
} else {
21+
res.add(t);
22+
}
23+
}
24+
h += height.get(j);
25+
n += cnt.get(j);
26+
i = j;
27+
}
28+
int[][] ans = new int[res.size()][3];
29+
for (i = 0; i < ans.length; ++i) {
30+
ans[i] = res.get(i);
31+
}
32+
return ans;
33+
}
34+
}

0 commit comments

Comments
 (0)