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] = [starti, endi, heighti]. This means that there is a building with heighti in the half-closed segment [starti, endi).
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] = [leftj, rightj, averagej] describes a half-closed segment [leftj, rightj) of the road where the average heights of the buildings in the segment is averagej.
buildings = [[1,5,2],[3,10,4]], the street could be represented by street = [[1,3,2],[3,5,3],[5,10,4]] because:
2 / 1 = 2.(2+4) / 2 = 3.4 / 1 = 4.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 <= 105buildings[i].length == 30 <= starti < endi <= 1081 <= heighti <= 105Problem Overview: You receive a list of buildings represented as [start, end, height]. Buildings overlap on a number line, and the goal is to split the street into minimal segments where the average height of active buildings stays constant. Each output segment should report [left, right, averageHeight] after considering all buildings covering that range.
Approach 1: Brute Force Coordinate Expansion (O(n * R) time, O(R) space)
A straightforward idea is to expand the entire coordinate range and compute the average height at every unit position. For each building, iterate from start to end and track the cumulative height and building count. After processing all buildings, compute the average height for every coordinate and merge adjacent positions with the same value into segments. This approach is simple but impractical when coordinates are large because the runtime depends on the numeric range R, not just the number of buildings.
Approach 2: Sweep Line with Sorted Events (O(n log n) time, O(n) space)
Instead of expanding every coordinate, treat each building boundary as an event. Create two events: one when a building starts and another when it ends. Sort all events by position using techniques similar to problems on sorting and sweep line processing. As you move left to right, maintain the current total height and active building count. Between two consecutive event positions, the average height remains constant, so you can emit a segment using avg = totalHeight / count. This significantly reduces work because only boundary points are processed.
Approach 3: Difference Array + Hash Table (O(n log n) time, O(n) space)
The optimal implementation uses a difference map to track how the active height sum and building count change at boundaries. For each building, update two entries in a hash table: at start add +height and +1 to the active count; at end subtract them. After collecting all updates, sort the keys and iterate through them in order. Maintain running totals of height sum and building count while scanning segments between consecutive coordinates. The average for a segment becomes heightSum / count. Merge adjacent segments if their averages match. This approach avoids expanding coordinates and processes only boundary changes, making it efficient and scalable. It commonly appears in interval problems involving arrays, sweep-line strategies, and event aggregation similar to skyline-style questions.
Recommended for interviews: The difference array + hash table sweep approach is the expected solution. Interviewers want to see that you convert interval updates into boundary events and maintain running aggregates. Mentioning the brute-force range expansion demonstrates baseline reasoning, but implementing the difference-map sweep shows strong understanding of interval processing and scalable algorithm design.
We can use the difference array concept, utilizing a hash table cnt to record the change in the number of buildings at each position, and another hash table d to record the change in height at each position.
Next, we sort the hash table d by its keys, use a variable s to record the current total height, and a variable m to record the current number of buildings.
Then, we traverse the hash table d. For each position, if m is not 0, it means there are buildings at the previous positions. We calculate the average height. If the average height of the buildings at the current position is the same as that of the previous buildings, we merge them; otherwise, we add the current position to the result set.
Finally, we return the result set.
The time complexity is O(n times log n), and the space complexity is O(n). Here, n is the number of buildings.
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Coordinate Expansion | O(n * R) | O(R) | Small coordinate ranges where expanding every position is feasible |
| Sweep Line with Sorted Events | O(n log n) | O(n) | General interval problems where boundaries define state changes |
| Difference Array + Hash Table | O(n log n) | O(n) | Optimal approach for large coordinate ranges and overlapping buildings |
2015. Average Height of Buildings in Each Segment (Leetcode Medium) • Programming Live with Larry • 270 views views
Practice Average Height of Buildings in Each Segment with our built-in code editor and test cases.
Practice on FleetCode