The Skyline Problem
Given a list of buildings, each described as [left, right, height], compute the skyline: the outer contour formed by all buildings viewed from a distance. Return it as a list of key points [x, height] sorted by x, where each point marks the left endpoint of a horizontal segment. The last point has height 0 to close the skyline, and no two consecutive segments may share the same height.
Open official problem prompt ↗Produce the outline of a city skyline: the sequence of x positions where the visible top height changes as you scan across all overlapping buildings.
Imagine walking east past a row of buildings and continuously noting the height of the tallest one blocking the sky. Each time that tallest silhouette changes, you jot down where and how high — that log is the skyline.
- Input
- buildings = [[2,9,10],[3,7,15],[5,12,12],[15,20,10],[19,24,8]]
- Output
- [[2,10],[3,15],[7,12],[12,0],[15,10],[20,8],[24,0]]
- Why
- The tallest active building at each x sets the height; the contour rises to 15 at x=3, drops as buildings end, and returns to 0 at x=12 and x=24.
1 <= buildings.length <= 10^40 <= left < right <= 2^31 - 11 <= height <= 2^31 - 1buildings is sorted by left in non-decreasing order