Imagine you're standing far away from a city, looking at its magnificent skyline. What you see is the outer contour - a silhouette formed by all the buildings against the horizon.

Given an array buildings where each building is represented as [left, right, height], your task is to return the key points that define this skyline.

What makes a key point?

  • It's where the skyline height changes
  • It's the left endpoint of each horizontal segment
  • No consecutive points should have the same height

The result should be a list of coordinates [[x1,y1], [x2,y2], ...] sorted by x-coordinate, ending with a point at height 0 where the rightmost building ends.

Example: Buildings [[2,9,10], [3,7,15], [5,12,12]] create a skyline with key points [[2,10], [3,15], [7,12], [12,0]]

Input & Output

example_1.py โ€” Basic skyline
$ Input: buildings = [[2,9,10],[3,7,15],[5,12,12]]
โ€บ Output: [[2,10],[3,15],[7,12],[12,0]]
๐Ÿ’ก Note: At x=2, building 1 starts (height 10). At x=3, building 2 starts (height 15, now max). At x=7, building 2 ends, height drops to 12 from building 3. At x=12, all buildings end.
example_2.py โ€” Single building
$ Input: buildings = [[0,2,3]]
โ€บ Output: [[0,3],[2,0]]
๐Ÿ’ก Note: Simple case: building starts at x=0 with height 3, ends at x=2 returning to ground level 0.
example_3.py โ€” Overlapping buildings
$ Input: buildings = [[0,2,3],[2,5,3]]
โ€บ Output: [[0,3],[5,0]]
๐Ÿ’ก Note: Two buildings of same height are adjacent. The skyline maintains height 3 from x=0 to x=5, so we don't add a key point at x=2 where they meet.

Visualization

Tap to expand
[150,120][200,170][270,140][370,100][420,0]๐Ÿ“ธ PhotographerSkyline Key PointsOnly capture where height changesLine Sweep Events:โ–ฒ Building starts โ†’ Add height to active setโ–ผ Building ends โ†’ Remove height from active set
Understanding the Visualization
1
Mark Critical Events
Identify where buildings start and end - these are the only points where skyline can change
2
Sweep the Horizon
Move from left to right, processing each critical event as you encounter it
3
Track Active Heights
Maintain a collection of currently 'active' building heights using a priority queue
4
Capture Key Moments
Whenever the maximum height changes, that's a key point worth photographing
Key Takeaway
๐ŸŽฏ Key Insight: The skyline only changes at building boundaries, so we process start/end events with a priority queue to efficiently track the maximum height among currently active buildings.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

Sorting events takes O(n log n), processing each event with heap operations takes O(log n) per event

n
2n
โšก Linearithmic
Space Complexity
O(n)

Space for events list and priority queue to store active building heights

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค buildings.length โ‰ค 104
  • 0 โ‰ค lefti < righti โ‰ค 231 - 1
  • 1 โ‰ค heighti โ‰ค 231 - 1
  • Buildings are given as [left, right, height] where left < right
Asked in
Google 45 Microsoft 38 Amazon 32 Meta 28
89.5K Views
Medium-High Frequency
~35 min Avg. Time
2.3K 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