Skip to content

Commit b8c1434

Browse files
Sync LeetCode submission Runtime - 77 ms (86.34%), Memory - 198.6 MB (88.95%)
1 parent f17ab10 commit b8c1434

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<p>You are given a 2D integer array <code>squares</code>. Each <code>squares[i] = [x<sub>i</sub>, y<sub>i</sub>, l<sub>i</sub>]</code> represents the coordinates of the bottom-left point and the side length of a square parallel to the x-axis.</p>
2+
3+
<p>Find the <strong>minimum</strong> y-coordinate value of a horizontal line such that the total area of the squares above the line <em>equals</em> the total area of the squares below the line.</p>
4+
5+
<p>Answers within <code>10<sup>-5</sup></code> of the actual answer will be accepted.</p>
6+
7+
<p><strong>Note</strong>: Squares <strong>may</strong> overlap. Overlapping areas should be counted <strong>multiple times</strong>.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
<div class="example-block">
13+
<p><strong>Input:</strong> <span class="example-io">squares = [[0,0,1],[2,2,1]]</span></p>
14+
15+
<p><strong>Output:</strong> <span class="example-io">1.00000</span></p>
16+
17+
<p><strong>Explanation:</strong></p>
18+
19+
<p><img alt="" src="https://assets.leetcode.com/uploads/2025/01/06/4062example1drawio.png" style="width: 378px; height: 352px;" /></p>
20+
21+
<p>Any horizontal line between <code>y = 1</code> and <code>y = 2</code> will have 1 square unit above it and 1 square unit below it. The lowest option is 1.</p>
22+
</div>
23+
24+
<p><strong class="example">Example 2:</strong></p>
25+
26+
<div class="example-block">
27+
<p><strong>Input:</strong> <span class="example-io">squares = [[0,0,2],[1,1,1]]</span></p>
28+
29+
<p><strong>Output:</strong> <span class="example-io">1.16667</span></p>
30+
31+
<p><strong>Explanation:</strong></p>
32+
33+
<p><img alt="" src="https://assets.leetcode.com/uploads/2025/01/15/4062example2drawio.png" style="width: 378px; height: 352px;" /></p>
34+
35+
<p>The areas are:</p>
36+
37+
<ul>
38+
<li>Below the line: <code>7/6 * 2 (Red) + 1/6 (Blue) = 15/6 = 2.5</code>.</li>
39+
<li>Above the line: <code>5/6 * 2 (Red) + 5/6 (Blue) = 15/6 = 2.5</code>.</li>
40+
</ul>
41+
42+
<p>Since the areas above and below the line are equal, the output is <code>7/6 = 1.16667</code>.</p>
43+
</div>
44+
45+
<p>&nbsp;</p>
46+
<p><strong>Constraints:</strong></p>
47+
48+
<ul>
49+
<li><code>1 &lt;= squares.length &lt;= 5 * 10<sup>4</sup></code></li>
50+
<li><code>squares[i] = [x<sub>i</sub>, y<sub>i</sub>, l<sub>i</sub>]</code></li>
51+
<li><code>squares[i].length == 3</code></li>
52+
<li><code>0 &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= 10<sup>9</sup></code></li>
53+
<li><code>1 &lt;= l<sub>i</sub> &lt;= 10<sup>9</sup></code></li>
54+
<li>The total area of all the squares will not exceed <code>10<sup>12</sup></code>.</li>
55+
</ul>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
double separateSquares(vector<vector<int>>& squares) {
4+
auto goUp = [&](double y) {
5+
double above=0, below=0;
6+
for (vector<int>& square : squares) {
7+
double yi = square[1], li = square[2];
8+
double h_below = min(li, max(0.0, y - yi));
9+
below += h_below * li;
10+
above += (li - h_below) * li;
11+
}
12+
return above > below;
13+
};
14+
15+
double l=0, r=2e9;
16+
while (r - l > 1e-5) {
17+
double m = l + (r - l) / 2;
18+
if (goUp(m)) l = m;
19+
else r = m;
20+
}
21+
22+
return r;
23+
}
24+
};

0 commit comments

Comments
 (0)