Falling Squares - Problem

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] = [left_i, sideLength_i] represents the i-th square with a side length of sideLength_i that is dropped with its left edge aligned with X-coordinate left_i.

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 i-th square.

Input & Output

Example 1 — Basic Falling Squares
$ Input: positions = [[1,2],[2,3],[6,1]]
Output: [2,5,5]
💡 Note: Square 1: [1,2] lands on ground, height = 2, max = 2. Square 2: [2,3] overlaps with Square 1, lands at height 2, new height = 2+3 = 5, max = 5. Square 3: [6,1] no overlap, lands on ground, height = 1, max = 5.
Example 2 — Stacking High
$ Input: positions = [[0,2],[1,2]]
Output: [2,4]
💡 Note: Square 1: [0,2] lands on ground, height = 2. Square 2: [1,2] overlaps Square 1 (range [1,3] overlaps [0,2]), lands on top, height = 2+2 = 4.
Example 3 — No Overlaps
$ Input: positions = [[0,1],[2,1],[4,1]]
Output: [1,1,1]
💡 Note: All squares land on ground separately with no overlaps. Each has height 1, so maximum remains 1.

Constraints

  • 1 ≤ positions.length ≤ 1000
  • 1 ≤ lefti ≤ 108
  • 1 ≤ sideLengthi ≤ 106

Visualization

Tap to expand
Falling Squares - Coordinate Compression + Segment Tree INPUT positions = [[1,2],[2,3],[6,1]] Squares to drop: 2x2 left=1 3x3 left=2 1x1 left=6 X-axis 0 1 2 3 6 Input Format: [left, sideLength] Square 1: [1, 2] Square 2: [2, 3] Square 3: [6, 1] ALGORITHM STEPS 1 Coordinate Compress Map coords: 1,3,2,5,6,7 to indices: 0,1,2,3,4,5 2 Build Segment Tree Track max height in each interval range 3 Process Each Square Query max in [L,R] New height = max + side 4 Update and Record Update segment tree Record global max Processing Example: Sq1[1,2]: max=0 --> h=0+2=2 Sq2[2,3]: max=2 --> h=2+3=5 Sq3[6,1]: max=0 --> h=0+1=1 Global max: 2, 5, 5 FINAL RESULT Final Configuration: 0 2 5 2x2 3x3 h=5 1x1 1 2 5 6 Output: [2, 5, 5] OK - Verified! Max heights after each drop: 2 --> 5 --> 5 Key Insight: Coordinate compression reduces the problem space. Instead of tracking every X position, we only track relevant boundaries (left edges and right edges of squares). Segment tree enables O(log n) range queries for max height and updates, giving O(n log n) total. TutorialsPoint - Falling Squares | Coordinate Compression + Segment Tree Approach
Asked in
Google 25 Amazon 20 Facebook 15
18.5K Views
Medium Frequency
~35 min Avg. Time
450 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen