oneby7

Range Queries Lazy propagation

615. Falling Squares

Hard

Squares are dropped one at a time onto a flat floor, the number line. You are given positions, where positions[i] = [left, sideLength] describes the i-th square: its left edge sits at left, and it is sideLength wide and sideLength tall.

Each square falls straight down until its bottom lands on the floor or on the top of an earlier square, and then it stays there. A square only lands on another square if they share some width. Two squares that just touch at an edge, such as one that ends at 5 and one that starts at 5, do not hold each other up.

After each drop, record the height of the tallest stack so far. Return these heights as a list, one per square, in the order the squares fall.

Example 1

Input: positions = [[1, 4], [3, 2], [6, 1], [5, 3]]

Output: [4,6,6,6]

Explanation: The first square covers 1 to 5 and is 4 tall. The second covers 3 to 5, lands on it and reaches 6. The third lands on the floor at 6 to 7, 1 tall. The fourth covers 5 to 8: it only touches the first two at 5, so it lands on the third and reaches 4. The tallest stack stays at 6.

Example 2

Input: positions = [[1, 2], [3, 2], [5, 2]]

Output: [2,2,2]

Explanation: Each square starts where the previous one ends. They only touch, so all three land on the floor.

Example 3

Input: positions = [[1000000000, 1000000], [1000000000, 1000000], [1, 1]]

Output: [1000000,2000000,2000000]

Explanation: The second square lands right on top of the first, at 2 × 106. The last one lands on the floor far to the left and is only 1 tall, so the tallest stack is still 2 × 106.

Check your understanding

Work out the answer for this input before you write any code.

positions = [[1, 5], [10, 2], [2, 1]]

Constraints

  • 1 ≤ positions.length ≤ 105
  • positions[i] = [left, sideLength]
  • 1 ≤ left ≤ 109
  • 1 ≤ sideLength ≤ 106

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.