The Skyline Problem

viaLeetCode

Problem: Given the locations and heights of a set of rectangular buildings, output the "skyline" formed by these buildings when viewed from a distance, represented as a list of key points [x, height] where the skyline height changes.

Constraints: Number of buildings up to 10^4.

Example: buildings = [[2,9,10],[3,7,15],[5,12,12],...] -> skyline key points like [[2,10],[3,15],[7,12],[12,0],...].

Approach: Use a sweep line over all building start/end x-coordinates combined with a max-heap (or ordered multiset) of currently "active" heights. At each event x, add the building's height if it starts here, or remove it if it ends here; after updating, if the current max active height differs from the previous output height, emit a new key point [x, newMaxHeight]. O(n log n).

Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account