Skip to content

Commit cfdfb66

Browse files
authored
Update 2015.Average-Height-of-Buildings-in-Each-Segment.cpp
1 parent 91c33cf commit cfdfb66

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

Others/2015.Average-Height-of-Buildings-in-Each-Segment/2015.Average-Height-of-Buildings-in-Each-Segment.cpp

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,38 @@ class Solution {
22
public:
33
vector<vector<int>> averageHeightOfBuildings(vector<vector<int>>& buildings)
44
{
5-
map<int,pair<int,int>>Map; // pos=>{sum, count}
5+
map<int, pair<int,int>>Map; // pos -> {diffHeight, diffCount}
66
for (auto build: buildings)
77
{
8-
int start = build[0], end = build[1], height = build[2];
9-
Map[start].first += height;
10-
Map[start].second += 1;
11-
Map[end].first -= height;
12-
Map[end].second -= 1;
13-
}
8+
int s = build[0], e = build[1], h = build[2];
9+
Map[s].first += h;
10+
Map[s].second += 1;
11+
Map[e].first -= h;
12+
Map[e].second -= 1;
13+
}
1414

15-
int sum = 0;
16-
int count = 0;
17-
vector<pair<int,int>>temp;
15+
vector<pair<int,int>>seg;
16+
int totalHeight = 0, totalCount = 0;
1817
for (auto& [pos, kv]: Map)
1918
{
20-
int heightDiff = kv.first, countDiff = kv.second;
21-
sum += heightDiff;
22-
count += countDiff;
23-
int avg = (count==0 ? 0 : sum / count);
24-
temp.push_back({pos, avg});
25-
}
19+
int diffHeight = kv.first, diffCount = kv.second;
20+
totalHeight += diffHeight;
21+
totalCount += diffCount;
22+
int avg = (totalCount ==0 ? 0: totalHeight / totalCount);
23+
seg.push_back({pos, avg});
24+
}
2625

2726
vector<vector<int>>rets;
28-
for (int i=0; i<temp.size(); i++)
27+
for (int i=0; i<seg.size(); i++)
2928
{
30-
if (temp[i].second==0) continue;
29+
if (seg[i].second == 0) continue;
3130
int j = i;
32-
while (j<temp.size() && temp[j].second == temp[i].second)
31+
while (j<seg.size() && seg[j].second == seg[i].second)
3332
j++;
34-
rets.push_back({temp[i].first, temp[j].first, temp[i].second});
35-
i = j-1;
33+
rets.push_back({seg[i].first, seg[j].first, seg[i].second});
34+
i = j-1;
3635
}
37-
return rets;
3836

37+
return rets;
3938
}
4039
};

0 commit comments

Comments
 (0)