#699

Falling Squares

candidate master · 1505 · lc hard +32 · verified · 47.4% accepted · 679 likes · top 32%

Description

Squares are dropped one by one onto the X-axis of a 2D plane. You receive a 2D array positions where positions[i] = [lefti, sideLengthi] describes a square with side length sideLengthi dropped with its left edge at X-coordinate lefti.

Each square falls straight down and rests on top of the highest overlapping surface below it (mere side contact does not count). Once landed, a square is stationary. After each drop, record the height of the tallest current stack.

Return an array ans where ans[i] is the maximum height after the ith square lands.

Example 1:

Input: positions = [[1,2],[2,3],[6,1]]
Output: [2,5,5]
Explanation:
After the first drop, the tallest stack is square 1 with a height of 2.
After the second drop, the tallest stack is squares 1 and 2 with a height of 5.
After the third drop, the tallest stack is still squares 1 and 2 with a height of 5.
Thus, we return an answer of [2, 5, 5].

Example 2:

Input: positions = [[100,100],[200,100]]
Output: [100,100]
Explanation:
After the first drop, the tallest stack is square 1 with a height of 100.
After the second drop, the tallest stack is either square 1 or square 2, both with heights of 100.
Thus, we return an answer of [100, 100].
Note that square 2 only brushes the right side of square 1, which does not count as landing on it.

Code

1
2
3