Ctrl K

City Block Outline

Hard
Seen from far away, the houses of a city merge into one silhouette. Every house is a rectangle standing on flat ground: buildings[i] = [left, right, height], where left and right are the x-coordinates of its left and right walls and height is how tall it is. The houses are listed in non-decreasing order of left. Describe the top outline of the silhouette as a list of points [x, y] in increasing order of x. Each point marks where the outline changes height: from x up to the next point the outline runs at height y. Bare ground between houses is part of the outline too (height 0). The last point always has height 0 and sits where the rightmost house ends. No two consecutive points may have the same height: if the outline stays at one height over several stretches in a row, that is one stretch with one point.
Example 1
buildings=[[1, 4, 6], [2, 7, 3], [9, 11, 5]] → [[1, 6], [4, 3], [7, 0], [9, 5], [11, 0]]

From 1 to 4 the outline runs at height 6, then only the second house (height 3) is visible up to 7, from 7 to 9 there is bare ground, and then the third house rises to 5 until 11.

Example 2
buildings=[[0, 3, 2], [3, 6, 2]] → [[0, 2], [6, 0]]

Two houses of the same height stand side by side, so the outline is a single flat segment from 0 to 6 and there is no point at 3.

Example 3
buildings=[[2, 8, 4], [3, 5, 9]] → [[2, 4], [3, 9], [5, 4], [8, 0]]

A tall house stands in front of a low one: the outline climbs from 4 to 9 between 3 and 5 and drops back to 4.

Constraints
  • 1 ≤ len(buildings) ≤ 10⁴
  • 0 ≤ left < right ≤ 2³¹ − 1
  • 1 ≤ height ≤ 2³¹ − 1
  • buildings is sorted by left in non-decreasing order
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSmaller Values to the Right HardWindow Sums Within a Band HardPairs With a Steep Drop HardA Dark Gap Between Street Lamps HardCost of Filling a Sorted Shelf Hard
loading Python…
Input
buildings=[[1, 4, 6], [2, 7, 3], [9, 11, 5]]
Expected
[[1, 6], [4, 3], [7, 0], [9, 5], [11, 0]]

Run checks the first 3 tests in your browser. Submit checks all 85 tests on the server, hidden ones included.