Ctrl K

Stacks of Dropped Crates

Hard
Square crates are dropped into a warehouse one at a time. crates[i] = [left, side] means crate i has side length side and falls with its left edge at coordinate left, so it covers the horizontal span from left to left + side. A crate falls straight down until it rests on the floor or on top of another crate, and then stays put. Crates that only touch along their vertical sides do not rest on each other. After every drop, record the height of the tallest stack in the warehouse. Return the list of those heights.
Example 1
crates=[[2, 3], [4, 2], [9, 1]] → [3, 5, 5]

The first crate covers [2, 5] and is 3 tall. The second one ([4, 6]) overlaps it and lands on top at height 3, so its top is at 5. The third one falls to the floor on its own and the maximum stays 5.

Example 2
crates=[[1, 4], [5, 4], [3, 2]] → [4, 4, 6]

The second crate only touches the side of the first one (spans [1, 5] and [5, 9]) and falls to the floor. The third one ([3, 5]) lands on the first: 4 + 2 = 6.

Constraints
  • 1 ≤ len(crates) ≤ 1000
  • 1 ≤ left ≤ 10⁸
  • 1 ≤ side ≤ 10⁶
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsCity Block Outline HardSmaller Values to the Right HardWindow Sums Within a Band HardPairs With a Steep Drop HardA Dark Gap Between Street Lamps Hard
loading Python…
Input
crates=[[2, 3], [4, 2], [9, 1]]
Expected
[3, 5, 5]

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