Average Height of Buildings in Each Segment - Problem

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.

For example, if buildings = [[1,5,2],[3,10,4]], the street could be represented by street = [[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.

Given buildings, return the 2D integer array street as described above (excluding any areas of the street where there are no buildings). You may return the array in any order.

Note: 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.

Input & Output

Example 1 — Basic Overlapping Buildings
$ Input: buildings = [[1,3,3],[2,4,4],[5,7,1]]
Output: [[1,2,3],[2,3,3],[3,4,4],[5,7,1]]
💡 Note: From 1 to 2: only building 1 with height 3. From 2 to 3: buildings 1 and 2, average = (3+4)/2 = 3. From 3 to 4: only building 2 with height 4. From 5 to 7: only building 3 with height 1.
Example 2 — Complete Overlap
$ Input: buildings = [[1,5,2],[3,10,4]]
Output: [[1,3,2],[3,5,3],[5,10,4]]
💡 Note: From 1 to 3: only first building (height 2). From 3 to 5: both buildings overlap, average = (2+4)/2 = 3. From 5 to 10: only second building (height 4).
Example 3 — Single Building
$ Input: buildings = [[0,2,3]]
Output: [[0,2,3]]
💡 Note: Only one building from 0 to 2 with height 3, so result is straightforward single segment.

Constraints

  • 1 ≤ buildings.length ≤ 1000
  • 0 ≤ starti < endi ≤ 108
  • 1 ≤ heighti ≤ 105

Visualization

Tap to expand
Average Height of Buildings in Each Segment INPUT Street with Buildings h=3 1 3 h=4 4 h=1 5 7 buildings array: [1,3,3] [2,4,4] [5,7,1] [start, end, height] Buildings 1 and 2 overlap! Segment [2,3) has both ALGORITHM STEPS 1 Create Events Start: +height, End: -height x=1:+3 x=2:+4 x=3:-3 x=4:-4 x=5:+1 x=7:-1 Sorted by x-coordinate 2 Sweep Line Process events left to right 3 Track Heights Sum heights, count buildings [1,2]: sum=3, cnt=1, avg=3 [2,3]: sum=7, cnt=2, avg=3.5-->3 [3,4]: sum=4, cnt=1, avg=4 [5,7]: sum=1, cnt=1, avg=1 4 Compute Average avg = floor(sum / count) Merge adjacent segments with same avg FINAL RESULT Averaged Segments 3 1 2 3 3 4 4 1 5 7 Output: [[1,2,3], [2,3,3], [3,4,4], [5,7,1]] OK - 4 segments [left, right, average] Key Insight: Event-Based Sweep Line Convert building intervals to start/end events. Process events in sorted order, tracking total height sum and building count. At each x-coordinate change, compute average = floor(sum/count) for the segment. This handles overlaps naturally - overlapping buildings both contribute to the average. TutorialsPoint - Average Height of Buildings in Each Segment | Event-Based Sweep Line Approach Time: O(n log n) | Space: O(n) where n = number of buildings
Asked in
Google 15 Amazon 12 Microsoft 8 Apple 5
23.5K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen