/**
* Problem: LeetCode 3453 - Separate Squares I
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public double separateSquares(int[][] squares) {
long totalArea = 0;
double low = 0.0;
double high = 0.0;
for (int[] sq : squares) {
long l = sq[2];
totalArea += l * l;
high = Math.max(high, (double) sq[1] + l);
}
double targetArea = totalArea / 2.0;
// Binary search for the minimum horizontal line y-coordinate
for (int iter = 0; iter < 80; iter++) {
double mid = low + (high - low) / 2.0;
if (getAreaBelow(squares, mid) >= targetArea) {
high = mid;
} else {
low = mid;
}
}
return low;
}
private double getAreaBelow(int[][] squares, double yLine) {
double area = 0.0;
for (int[] sq : squares) {
double y = sq[1];
double l = sq[2];
if (yLine > y) {
double h = Math.min(l, yLine - y);
area += h * l;
}
}
return area;
}
}
"""
Problem: LeetCode 3453 - Separate Squares I
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
from typing import List
class Solution:
def separateSquares(self, squares: List[List[int]]) -> float:
total_area = sum(l * l for _, _, l in squares)
target_area = total_area / 2.0
low = 0.0
high = float(max(y + l for _, y, l in squares))
def get_area_below(y_line: float) -> float:
area = 0.0
for _, y, l in squares:
if y_line > y:
h = min(float(l), y_line - y)
area += h * l
return area
# 80 iterations of binary search for high precision
for _ in range(80):
mid = low + (high - low) / 2.0
if get_area_below(mid) >= target_area:
high = mid
else:
low = mid
return low
/**
* Problem: LeetCode 3453 - Separate Squares I
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <algorithm>
class Solution {
public:
double separateSquares(std::vector<std::vector<int>>& squares) {
long long totalArea = 0;
double low = 0.0;
double high = 0.0;
for (const auto& sq : squares) {
long long l = sq[2];
totalArea += l * l;
high = std::max(high, static_cast<double>(sq[1] + l));
}
double targetArea = totalArea / 2.0;
// Binary search for minimum horizontal line y-coordinate
for (int iter = 0; iter < 80; ++iter) {
double mid = low + (high - low) / 2.0;
if (getAreaBelow(squares, mid) >= targetArea) {
high = mid;
} else {
low = mid;
}
}
return low;
}
private:
double getAreaBelow(const std::vector<std::vector<int>>& squares, double yLine) {
double area = 0.0;
for (const auto& sq : squares) {
double y = sq[1];
double l = sq[2];
if (yLine > y) {
double h = std::min(l, yLine - y);
area += h * l;
}
}
return area;
}
};
/**
* Problem: LeetCode 3453 - Separate Squares I
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
/**
* @param {number[][]} squares
* @return {number}
*/
var separateSquares = function(squares) {
let totalArea = 0;
let low = 0;
let high = 0;
for (let i = 0; i < squares.length; i++) {
const y = squares[i][1];
const l = squares[i][2];
totalArea += l * l;
high = Math.max(high, y + l);
}
const targetArea = totalArea / 2;
function getAreaBelow(yLine) {
let area = 0;
for (let i = 0; i < squares.length; i++) {
const y = squares[i][1];
const l = squares[i][2];
if (yLine > y) {
const h = Math.min(l, yLine - y);
area += h * l;
}
}
return area;
}
// 80 iterations of binary search for high precision
for (let iter = 0; iter < 80; iter++) {
const mid = low + (high - low) / 2;
if (getAreaBelow(mid) >= targetArea) {
high = mid;
} else {
low = mid;
}
}
return low;
};