Average Height of Buildings in Each Segment - Problem

Imagine you're a city planner analyzing building heights along a perfectly straight street! ๐Ÿ™๏ธ

You're given a 2D array buildings where each buildings[i] = [start, end, height] represents a building with height covering the half-closed interval [start, end) on the street.

Your task is to divide the street into segments where each segment has a constant average building height. Return these segments as [[left, right, average], ...] using the minimum number of non-overlapping segments.

Key Points:

  • Half-closed interval [a, b) includes a but excludes b
  • Average is calculated using integer division
  • Only include segments where buildings exist
  • Segments can be returned in any order

Example: If buildings = [[1,5,2],[3,10,4]]
โ†’ Street segments: [[1,3,2], [3,5,3], [5,10,4]]
โ†’ From 1-3: only first building (avg = 2/1 = 2)
โ†’ From 3-5: both buildings (avg = (2+4)/2 = 3)
โ†’ From 5-10: only second building (avg = 4/1 = 4)

Input & Output

example_1.py โ€” Basic Case
$ Input: buildings = [[1,5,2],[3,10,4]]
โ€บ Output: [[1,3,2],[3,5,3],[5,10,4]]
๐Ÿ’ก Note: From 1-3: only building 1 exists (avg=2). From 3-5: both buildings exist (avg=(2+4)/2=3). From 5-10: only building 2 exists (avg=4).
example_2.py โ€” Multiple Overlaps
$ Input: buildings = [[1,4,2],[3,9,4],[6,7,1]]
โ€บ Output: [[1,3,2],[3,4,3],[4,6,4],[6,7,2],[7,9,4]]
๐Ÿ’ก Note: Complex overlapping pattern creates 5 distinct segments with different average heights.
example_3.py โ€” Single Building
$ Input: buildings = [[0,2,3]]
โ€บ Output: [[0,2,3]]
๐Ÿ’ก Note: With only one building, the entire segment has that building's height as the average.

Constraints

  • 1 โ‰ค buildings.length โ‰ค 1000
  • buildings[i].length == 3
  • 0 โ‰ค starti < endi โ‰ค 108
  • 1 โ‰ค heighti โ‰ค 1000
  • Half-closed intervals: [start, end) includes start but excludes end

Visualization

Tap to expand
๐Ÿ—๏ธ Building Height AnalysisStreet Coordinate LineBuilding A[2, 5] h=2Building B[4, 8] h=42458[2,4]: avg=2[4,5]: avg=3[5,8]: avg=4โšก Optimal Algorithm1. Critical points: {2, 4, 5, 8}2. Segments: [2,4], [4,5], [5,8]3. Averages: 2, (2+4)/2=3, 4โฐ Time: O(N log N) vs O(Nร—R)
Understanding the Visualization
1
Identify Critical Points
Mark all building start and end positions - these are where average heights change
2
Sort Boundary Points
Arrange all critical coordinates in ascending order for sweep line processing
3
Sweep and Calculate
Move through each segment, count overlapping buildings, and compute average heights
4
Generate Result
Output segments as [left, right, average] tuples for city planning report
Key Takeaway
๐ŸŽฏ Key Insight: Average heights only change at building boundaries. By processing just these critical points instead of every coordinate, we achieve optimal O(N log N) performance!
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18
68.4K Views
Medium-High Frequency
~25 min Avg. Time
1.8K 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