Skip to content
Problem 218 · LeetCode 218(opens in a new tab)

The Skyline Problem

HardArrays

Each building is a rectangle standing on flat ground, given as [left, right, height]. The list buildings is sorted by left. Seen from far away, the buildings together form one outline against the sky.

Return that outline as a list of key points [x, y], sorted by x. Each key point is where the outline's height changes to y; the final point has y = 0 and marks where the last building ends. Ground between buildings is part of the outline. Two neighbouring points must never have the same height.

Example 1

Input:  buildings = [[1,6,5],[3,9,7],[11,14,4]]
Output: [[1,5],[3,7],[9,0],[11,4],[14,0]]
Why:    the 7 hides the end of the 5; ground shows from 9 to 11
2 more examples and the constraints

Example 2

Input:  buildings = [[2,4,6],[4,7,6]]
Output: [[2,6],[7,0]]
Why:    they touch at the same height, so it is one flat roof

Example 3

Input:  buildings = [[1,10,3],[2,5,3]]
Output: [[1,3],[10,0]]
Why:    the second building hides inside the first

Constraints

  • 1 ≤ buildings.length ≤ 10,000
  • 0 ≤ left < right ≤ 2,147,483,647 (2³¹ − 1)
  • 1 ≤ height ≤ 2,147,483,647
  • buildings is sorted by left, smallest first

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Check every edgeO(n²)
  2. Sweep line with a max-heapO(n log n)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for 10,000 buildingsVerdict
Check every edgeO(n²)
Correct; too slow at the limit
Sweep line with a max-heapO(n log n)
Best; standard answer

n = number of buildings.

Edge cases to test

  • One building. [[0,3,2]] gives [[0,2],[3,0]].
  • Touching, same height. [2,4,6] and [4,7,6] make one roof: no point at 4.
  • Same left edge. Only the taller start may add a point at that x.
  • Hidden building. A building inside a taller one adds no points.
  • Huge coordinates. Edges and heights reach 2³¹ − 1, so a sentinel of "infinity" as an int can collide with a real edge.

Common mistakes

  • Treating a building as covering its own right edge, which adds a point one step late.
  • Appending a point for every event, which leaves repeated heights at the same x or in a row.
  • Removing ended buildings from the middle of a heap in O(n) instead of popping stale tops lazily.

How to talk through it

  1. 1Clarify. Do touching buildings of equal height merge? Is the input sorted? Can coordinates be huge?
  2. 2Find the key fact. The height can only change at an edge, so only edges matter.
  3. 3Give the baseline. For each edge, scan all buildings: O(n²).
  4. 4Sweep with a heap. Starts push, stale tops are popped lazily, and the top is the height.
  5. 5Test out loud. Run a gap, a hidden building, and two touching roofs of the same height.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?