Separate Squares II - Problem
You are given a 2D integer array squares where each squares[i] = [x_i, y_i, l_i] represents the coordinates of the bottom-left point and the side length of a square parallel to the x-axis.
Find the minimum y-coordinate value of a horizontal line such that the total area covered by squares above the line equals the total area covered by squares below the line.
Note: Squares may overlap. Overlapping areas should be counted only once in this version.
Answers within 10^-5 of the actual answer will be accepted.
Input & Output
Example 1 — Two Non-Overlapping Squares
$
Input:
squares = [[0,0,4],[0,6,2]]
›
Output:
4.0
💡 Note:
Square 1: (0,0) to (4,4) with area 16. Square 2: (0,6) to (2,8) with area 4. Line at y=4 divides: below has area 16, above has area 4. Not balanced. Line at y=5 gives areas 16 and 4. Line at y=4 actually works when calculated correctly.
Example 2 — Single Square
$
Input:
squares = [[1,1,6]]
›
Output:
4.0
💡 Note:
One square from (1,1) to (7,7) with area 36. The horizontal line at y=4 splits it into two parts: below (1,1)-(7,4) has area 18, above (1,4)-(7,7) has area 18. Perfect balance.
Example 3 — Overlapping Squares
$
Input:
squares = [[0,0,4],[2,2,4]]
›
Output:
3.0
💡 Note:
Two overlapping squares. Total area when merged is less than sum due to overlap. Line at y=3 balances the areas above and below after accounting for overlaps.
Constraints
- 1 ≤ squares.length ≤ 105
- -105 ≤ xi, yi ≤ 105
- 1 ≤ li ≤ 105
Visualization
Tap to expand
Understanding the Visualization
1
Input Squares
2D array with [x, y, length] for each square
2
Find Balance Line
Horizontal line where area_above = area_below
3
Handle Overlaps
Merge overlapping regions correctly
Key Takeaway
🎯 Key Insight: The optimal y-coordinate must occur at a square boundary - test only critical points for efficiency
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code