-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
average-height-of-buildings-in-each-segment.cpp
64 lines (62 loc) · 2.04 KB
/
average-height-of-buildings-in-each-segment.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Time: O(nlogn)
// Space: O(n)
class Solution {
public:
vector<vector<int>> averageHeightOfBuildings(vector<vector<int>>& buildings) {
vector<tuple<int, int, int>> points;
for (const auto& b : buildings) {
points.emplace_back(b[0], 1, b[2]);
points.emplace_back(b[1], -1, b[2]);
}
sort(begin(points), end(points));
vector<vector<int>> result;
int total = 0, cnt = 0;
int prev = -1;
for (const auto& [curr, c, h] : points) {
if (cnt && prev != curr) {
if (!empty(result) && result.back()[1] == prev && result.back()[2] == total / cnt) {
result.back()[1] = curr;
} else {
result.push_back({prev, curr, total / cnt});
}
}
total += h * c;
cnt += c;
prev = curr;
}
return result;
}
};
// Time: O(nlogn)
// Space: O(n)
class Solution2 {
public:
vector<vector<int>> averageHeightOfBuildings(vector<vector<int>>& buildings) {
unordered_map<int, pair<int, int>> count;
for (const auto& b : buildings) {
count[b[0]] = {count[b[0]].first + 1, count[b[0]].second + b[2]};
count[b[1]] = {count[b[1]].first - 1, count[b[1]].second - b[2]};
}
vector<tuple<int, int, int>> points;
for (const auto& [k, v] : count) {
points.emplace_back(k, v.first, v.second);
}
sort(begin(points), end(points));
vector<vector<int>> result;
int total = 0, cnt = 0;
int prev = -1;
for (const auto& [curr, c, h] : points) {
if (cnt) {
if (!empty(result) && result.back()[1] == prev && result.back()[2] == total / cnt) {
result.back()[1] = curr;
} else {
result.push_back({prev, curr, total / cnt});
}
}
total += h;
cnt += c;
prev = curr;
}
return result;
}
};