Falling Squares - Problem

Imagine you're building a digital Tetris-like simulation where squares are dropped one by one onto a 2D plane! ๐ŸŽฎ

You're given a series of squares that fall from above and land on the X-axis or on top of other squares. Each square is defined by its left edge position and side length. The squares fall straight down until they hit either the ground (X-axis) or another square below them.

Key Rules:

  • Squares fall one at a time in the order given
  • A square stops falling when it hits the X-axis or lands on top of another square
  • Squares only "land on" other squares if they overlap vertically - brushing sides doesn't count
  • Once landed, squares freeze in place forever

Your Mission: After each square lands, report the maximum height of all stacked squares. Return an array where result[i] is the tallest stack height after dropping the i-th square.

Input: positions[i] = [leftEdge, sideLength] - the left coordinate and size of each square

Output: Array of maximum heights after each drop

Input & Output

example_1.py โ€” Basic Square Dropping
$ Input: positions = [[1,2],[4,3],[2,2]]
โ€บ Output: [2, 5, 5]
๐Ÿ’ก Note: First square [1,2] lands on ground: height = 2. Second square [4,3] lands on ground (no overlap): height = 3, max = 5. Third square [2,2] overlaps with first square, lands on top: height = 2+2 = 4, max stays 5.
example_2.py โ€” Single Square
$ Input: positions = [[100,100]]
โ€บ Output: [100]
๐Ÿ’ก Note: Only one square drops and lands on the X-axis at height 100. The maximum height is 100.
example_3.py โ€” Overlapping Stack
$ Input: positions = [[0,2],[1,1],[0,1]]
โ€บ Output: [2, 3, 3]
๐Ÿ’ก Note: First square [0,2] creates height 2. Second square [1,1] overlaps and stacks: height 2+1=3. Third square [0,1] also overlaps first square, creating another stack of height 2+1=3. Max remains 3.

Visualization

Tap to expand
Ground (X-axis)Square 1Sq 2Sq 34New SquareFalling...Checks overlap with existing squaresLands on highest overlapping squareCurrent Max Height: 100Algorithm Steps:1. Drop square vertically2. Find overlapping squares3. Stack on highest one
Understanding the Visualization
1
Drop the square
Each square falls vertically until it hits something
2
Check for collisions
Find all existing squares that overlap horizontally
3
Find landing height
Land on the highest overlapping square (or ground if none)
4
Update maximum
Track the tallest tower height after each drop
Key Takeaway
๐ŸŽฏ Key Insight: Use coordinate compression to handle large coordinate ranges efficiently, then segment trees for fast range queries - reducing time from O(nยฒ) to O(n log n)

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

For each of n squares, we check overlap with all previously dropped squares

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Store information for all n dropped squares

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค positions.length โ‰ค 1000
  • 1 โ‰ค lefti โ‰ค 108
  • 1 โ‰ค sideLengthi โ‰ค 106
  • All squares have positive side lengths
  • Coordinates can be very large, requiring coordinate compression
Asked in
Google 35 Facebook 28 Microsoft 22 Amazon 18
28.4K Views
Medium Frequency
~25 min Avg. Time
892 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