You are given a 2D integer array squares. Each squares[i] = [xi, yi, li]
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.
Answers within 10-5 of the actual answer will be accepted.
Note : Squares may overlap. Overlapping areas should be counted only once in this version.
Input: squares =[[0,0,1],[2,2,1]]Output: 1.00000Explanation:

Any horizontal line between `y = 1` and `y = 2` results in an equal split,with1 square unit above and 1 square unit below. The minimum y-value is1.
Input: squares =[[0,0,2],[1,1,1]]Output: 1.00000Explanation:

Since the blue square overlaps with the red square, it will not be counted
again. Thus, the line `y = 1` splits the squares into two equal parts.
We want to find the minimum y such that the area above and below y is equal, counting overlapping areas only once. We can use a line sweep to compute the union area below any y, and binary search to find the y where the area below is half the total area.
classSolution:
defseparateSquares(self, squares: list[list[int]]) -> float:
xs = set()
for x, y, l in squares:
xs.add(x)
xs.add(x + l)
xs = sorted(xs)
defarea(y):
res =0for i in range(len(xs) -1):
segs = []
for x0, y0, l in squares:
if x0 <= xs[i] and xs[i+1] <= x0 + l:
yl, yr = y0, y0 + l
if yl < y:
segs.append((yl, min(y, yr)))
ifnot segs: continue segs.sort()
last =-1e20 sum_ =0for a, b in segs:
if a > last:
sum_ += b - a
last = b
elif b > last:
sum_ += b - last
last = b
res += sum_ * (xs[i+1] - xs[i])
return res
total = area(1e9+1)
l, r =0, 1e9+1for _ in range(50):
m = (l + r) /2if area(m) *2< total:
l = m
else:
r = m
return l
⏰ Time complexity: O(n log n + K log M), where n = number of squares, K = number of unique x-intervals, M = search range. Each area computation is O(nK), binary search is log M steps.
🧺 Space complexity: O(n + K), for storing x-coordinates and intervals.