The Skyline Problem

Hard Intervals

buildings lists buildings standing on flat ground, each [left, right, height]: a rectangle from left to right along the ground, height tall. They come in no particular order, and may touch, overlap or stand inside one another.

The skyline is the outline they make together, seen from far away. At each point along the ground its height is the height of the tallest building standing there, where a building stands from its left up to but not including its right; where none stands, the outline is on the ground, at height 0.

Return the skyline's key points [x, height] from left to right: one at each x where the outline's height changes, giving its new height, the last one where it comes down to the ground. No two points in a row have the same height.

Example 1

Input: buildings = [[1,5,4],[3,8,6],[10,12,2]]
Output: [[1,4],[3,6],[8,0],[10,2],[12,0]]

The first building rises to 4 at 1. At 3 the second, 6 tall, rises above it and stands until 8, past the first one's end at 5, so the outline drops to the ground at 8. The third building stands alone from 10 to 12.

Example 2

Input: buildings = [[4,6,2],[1,4,2],[6,9,3]]
Output: [[1,2],[6,3],[9,0]]

Example 3

Input: buildings = [[2,10,3],[4,6,7]]
Output: [[2,3],[4,7],[6,3],[10,0]]

Constraints

  • 1 ≤ buildings.length ≤ 5 × 10⁴
  • buildings[i] = [left, right, height]
  • 0 ≤ left < right ≤ 10⁹
  • 1 ≤ height ≤ 10⁹

Practice this in an AI round

30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.

See the AI round

Watch the explanation

More Intervals

All Intervals problems