Skip to content

2015. Average Height of Buildings in Each Segment

  • Time: $O(\texttt{sort})$
  • Space: $O(n)$
 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
class Solution {
 public:
  vector<vector<int>> averageHeightOfBuildings(vector<vector<int>>& buildings) {
    vector<vector<int>> ans;
    vector<pair<int, int>> events;

    for (const vector<int>& b : buildings) {
      const int start = b[0];
      const int end = b[1];
      const int height = b[2];
      events.emplace_back(start, height);
      events.emplace_back(end, -height);
    }

    ranges::sort(events);

    int prev = 0;
    int count = 0;
    int sumHeight = 0;

    for (const auto& [curr, height] : events) {
      if (sumHeight > 0 && curr > prev) {
        const int avgHeight = sumHeight / count;
        if (!ans.empty() && ans.back()[1] == prev && avgHeight == ans.back()[2])
          ans.back()[1] = curr;
        else
          ans.push_back({prev, curr, avgHeight});
      }
      sumHeight += height;
      count += height > 0 ? 1 : -1;
      prev = curr;
    }

    return ans;
  }
};
 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
class Solution {
  public int[][] averageHeightOfBuildings(int[][] buildings) {
    List<int[]> ans = new ArrayList<>();
    List<Pair<Integer, Integer>> events = new ArrayList<>();

    for (int[] b : buildings) {
      final int start = b[0];
      final int end = b[1];
      final int height = b[2];
      events.add(new Pair<>(start, height));
      events.add(new Pair<>(end, -height));
    }

    Collections.sort(events, Comparator.comparing(Pair::getKey));

    int prev = 0;
    int count = 0;
    int sumHeight = 0;

    for (Pair<Integer, Integer> event : events) {
      final int curr = event.getKey();
      final int height = event.getValue();
      if (sumHeight > 0 && curr > prev) {
        final int avgHeight = sumHeight / count;
        if (!ans.isEmpty() && ans.get(ans.size() - 1)[1] == prev &&
            avgHeight == ans.get(ans.size() - 1)[2])
          ans.get(ans.size() - 1)[1] = curr;
        else
          ans.add(new int[] {prev, curr, avgHeight});
      }
      sumHeight += height;
      count += height > 0 ? 1 : -1;
      prev = curr;
    }

    return ans.stream().toArray(int[][] ::new);
  }
}
 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
class Solution:
  def averageHeightOfBuildings(self, buildings: list[list[int]]) -> list[list[int]]:
    ans = []
    events = []

    for start, end, height in buildings:
      events.append((start, height))
      events.append((end, -height))

    prev = 0
    count = 0
    sumHeight = 0

    for curr, height in sorted(events):
      if sumHeight > 0 and curr > prev:
        avgHeight = sumHeight // count
        if ans and ans[-1][1] == prev and avgHeight == ans[-1][2]:
          ans[-1][1] = curr
        else:
          ans.append([prev, curr, avgHeight])
      sumHeight += height
      count += 1 if height > 0 else -1
      prev = curr

    return ans