Skip to the code
cf.
+50 XP

#590 Outline of Overlapping Cards

hard

A dashboard draws rectangular cards that all sit on the same baseline. Card i is [left, right, height]: it covers x from left up to right and rises height units. Cards overlap freely. The designer wants the outline of the whole group seen from the side.

Write outline(cards) that returns the outline as key points [x, h], sorted by x: each key point says that from x onward the outline's height is h, until the next key point. Include a point wherever the height changes, never two points in a row with the same height, and end with a point of height 0 where the last card ends. Return [] for no cards.

Example 1

Input: cards = [[2,9,10],[3,7,15],[5,12,12],[15,20,10],[19,24,8]]
Output: [[2,10],[3,15],[7,12],[12,0],[15,10],[20,8],[24,0]]

For example, from 7 the tallest card is the one of height 12, until it ends at 12.

Example 2

Input: cards = [[0,2,3],[2,5,3]]
Output: [[0,3],[5,0]]

Two touching cards of the same height make one flat stretch.

Constraints

  • 0 <= cards.length <= 10000
  • 0 <= left < right <= 10^9
  • 1 <= height <= 10^9
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).