Skip to content

Practice Falling Squares

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#699
LeetCode Problem

Falling Squares

Problem Statement

There are several squares being dropped onto the X-axis of a 2D plane. You are given a 2D integer array positions where positions[i] = [lefti, sideLengthi] represents the ith square with a side length of sideLengthi that is dropped with its left edge aligned with X-coordinate lefti. Each square is dropped one at a time from a height above any landed squares. It then falls downward (negative Y direction) until it either lands on the top side of another square or on the X-axis. A square brushing the left/right side of another square does not count as landing on it. Once it lands, it freezes in place and cannot be moved. After each square is dropped, you must record the height of the current tallest stack of squares. Return an integer array ans where ans[i] represents the height described above after dropping the ith square.

Source: Falling Squares on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
positions = [[1,2],[2,3],[6,1]]
Output
[2,5,5]

# Example 2

Input
positions = [[100,100],[200,100]]
Output
[100,100]

Constraints

1 <= positions.length <= 1000
1 <= lefti <= 10^8
1 <= sideLengthi <= 10^6

Related LeetCode Problems

Practice more problems similar to Falling Squares.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.