Average Height of Buildings in Each Segment Problem
Description
LeetCode Problem 2015.
A perfectly straight street is represented by a number line. The street has building(s) on it and is represented by a 2D integer array buildings, where buildings[i] = [start_i, end_i, height_i]. This means that there is a building with height_i in the half-closed segment [start_i, end_i).
You want to describe the heights of the buildings on the street with the minimum number of non-overlapping segments. The street can be represented by the 2D integer array street where street[j] = [left_j, right_j, average_j] describes a half-closed segment [left_j, right_j) of the road where the average heights of the buildings in the segment is average_j.
- For example, if
buildings = [[1,5,2],[3,10,4]],the street could be represented bystreet = [[1,3,2],[3,5,3],[5,10,4]]because:- From 1 to 3, there is only the first building with an average height of
2 / 1 = 2. - From 3 to 5, both the first and the second building are there with an average height of
(2+4) / 2 = 3. - From 5 to 10, there is only the second building with an average height of
4 / 1 = 4.
- From 1 to 3, there is only the first building with an average height of
Given buildings, return the 2D integer array street as described above (excluding any areas of the street where there are no buldings). You may return the array in any order.
The average of n elements is the sum of the n elements divided (integer division) by n.
A half-closed segment [a, b) is the section of the number line between points a and b including point a and not including point b.
Example 1:

Input: buildings = [[1,4,2],[3,9,4]]
Output: [[1,3,2],[3,4,3],[4,9,4]]
Explanation:
From 1 to 3, there is only the first building with an average height of 2 / 1 = 2.
From 3 to 4, both the first and the second building are there with an average height of (2+4) / 2 = 3.
From 4 to 9, there is only the second building with an average height of 4 / 1 = 4.
Example 2:
Input: buildings = [[1,3,2],[2,5,3],[2,8,3]]
Output: [[1,3,2],[3,8,3]]
Explanation:
From 1 to 2, there is only the first building with an average height of 2 / 1 = 2.
From 2 to 3, all three buildings are there with an average height of (2+3+3) / 3 = 2.
From 3 to 5, both the second and the third building are there with an average height of (3+3) / 2 = 3.
From 5 to 8, there is only the last building with an average height of 3 / 1 = 3.
The average height from 1 to 3 is the same so we can group them into one segment.
The average height from 3 to 8 is the same so we can group them into one segment.
Example 3:
Input: buildings = [[1,2,1],[5,6,1]]
Output: [[1,2,1],[5,6,1]]
Explanation:
From 1 to 2, there is only the first building with an average height of 1 / 1 = 1.
From 2 to 5, there are no buildings, so it is not included in the output.
From 5 to 6, there is only the second building with an average height of 1 / 1 = 1.
We cannot group the segments together because an empty space with no buildings seperates the segments.
Constraints:
1 <= buildings.length <= 10^5buildings[i].length == 30 <= start_i < end_i <= 10^81 <= height_i <= 10^5
Solution
Approach: Difference Array + Sorting
A building adds its height and a count of one at its start, and takes them away again at its end. So we record two changes per position in hash maps: cnt for the number of buildings and d for the total height.
Sweep the positions in increasing order, keeping the running total height s and the running number of buildings m. When we reach a position k and m > 0, the stretch from the previous position last to k is covered, and its average height is s / m (integer division). If the last segment in the answer ends at last and has the same average, extend it to k; otherwise start a new segment [last, k). Then apply the changes at k and move on.
Gaps with no building have m = 0, so no segment is produced for them and segments on either side of a gap are never merged.
Complexity: O(n log n) time because of the sort, and O(n) space, where n is the number of buildings.
from collections import defaultdict
class Solution:
def averageHeightOfBuildings(self, buildings: List[List[int]]) -> List[List[int]]:
cnt = defaultdict(int)
d = defaultdict(int)
for start, end, height in buildings:
cnt[start] += 1
cnt[end] -= 1
d[start] += height
d[end] -= height
s = m = 0
last = -1
ans = []
for k, v in sorted(d.items()):
if m:
avg = s // m
if ans and ans[-1][2] == avg and ans[-1][1] == last:
ans[-1][1] = k
else:
ans.append([last, k, avg])
s += v
m += cnt[k]
last = k
return ans