3453. Separate Squares I
Description
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 of the squares above the line equals the total area of the squares below the line.
Answers within 10-5 of the actual answer will be accepted.
Note: Squares may overlap. Overlapping areas should be counted multiple times.
Example 1:
Input: squares = [[0,0,1],[2,2,1]]
Output: 1.00000
Explanation:
Any horizontal line between y = 1 and y = 2 will have 1 square unit above it and 1 square unit below it. The lowest option is 1.
Example 2:
Input: squares = [[0,0,2],[1,1,1]]
Output: 1.16667
Explanation:
The areas are:
- Below the line:
7/6 * 2 (Red) + 1/6 (Blue) = 15/6 = 2.5. - Above the line:
5/6 * 2 (Red) + 5/6 (Blue) = 15/6 = 2.5.
Since the areas above and below the line are equal, the output is 7/6 = 1.16667.
Constraints:
1 <= squares.length <= 5 * 104squares[i] = [xi, yi, li]squares[i].length == 30 <= xi, yi <= 1091 <= li <= 109- The total area of all the squares will not exceed
1012.
Solutions
Solution 1: Binary Search
Thinking
A horizontal line must bisect the total area of the squares. Area below the line grows monotonically; the total area is up to \(10^{12}\) and coordinates are large, so heights cannot be enumerated.
Monotonicity on the reals suggests binary search. The predicate asks whether the area strictly below \(y=y_1\) already reaches half.
A square whose bottom is under \(y_1\) contributes side length times the clipped depth. We search until the gap is \(10^{-5}\) and return the right end.
According to the problem, we need to find a horizontal line such that the total area of squares above the line equals the total area of squares below the line. Since as the \(y\) coordinate increases, the area below the line increases and the area above the line decreases, we can use binary search to find the \(y\) coordinate of this horizontal line.
We define the left boundary of the binary search as \(l = 0\), and the right boundary as \(r = \max(y_i + l_i)\), which is the highest point of all squares. Then we calculate the midpoint \(mid = (l + r) / 2\) and calculate the area below this horizontal line. If this area is greater than or equal to half of the total area, it means we need to move the right boundary \(r\) downward; otherwise, we move the left boundary \(l\) upward. We repeat this process until the difference between the left and right boundaries is less than a very small value (e.g., \(10^{-5}\)).
The time complexity is \(O(n \log(MU))\), where \(n\) is the number of squares, \(M = 10^5\), and \(U = \max(y_i + l_i)\). The space complexity is \(O(1)\).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |

