Skip to content

Commit 0207b81

Browse files
authored
feat: add Rust solution for lc No.3453 (#4966)
1 parent 2aed09b commit 0207b81

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

solution/3400-3499/3453.Separate Squares I/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,49 @@ function separateSquares(squares: number[][]): number {
274274
}
275275
```
276276

277+
#### Rust
278+
279+
```rust
280+
impl Solution {
281+
pub fn separate_squares(squares: Vec<Vec<i32>>) -> f64 {
282+
let mut s: f64 = 0.0;
283+
284+
let mut l: f64 = 0.0;
285+
let mut r: f64 = 0.0;
286+
287+
for a in squares.iter() {
288+
let len = a[2] as f64;
289+
s += len * len;
290+
r = r.max((a[1] + a[2]) as f64);
291+
}
292+
293+
let check = |y1: f64| -> bool {
294+
let mut t: f64 = 0.0;
295+
for a in squares.iter() {
296+
let y = a[1] as f64;
297+
let l = a[2] as f64;
298+
if y < y1 {
299+
let h = l.min(y1 - y);
300+
t += l * h;
301+
}
302+
}
303+
t >= s / 2.0
304+
};
305+
306+
const EPS: f64 = 1e-5;
307+
while r - l > EPS {
308+
let mid = (l + r) / 2.0;
309+
if check(mid) {
310+
r = mid;
311+
} else {
312+
l = mid;
313+
}
314+
}
315+
r
316+
}
317+
}
318+
```
319+
277320
<!-- tabs:end -->
278321

279322
<!-- solution:end -->

solution/3400-3499/3453.Separate Squares I/README_EN.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,49 @@ function separateSquares(squares: number[][]): number {
272272
}
273273
```
274274

275+
#### Rust
276+
277+
```rust
278+
impl Solution {
279+
pub fn separate_squares(squares: Vec<Vec<i32>>) -> f64 {
280+
let mut s: f64 = 0.0;
281+
282+
let mut l: f64 = 0.0;
283+
let mut r: f64 = 0.0;
284+
285+
for a in squares.iter() {
286+
let len = a[2] as f64;
287+
s += len * len;
288+
r = r.max((a[1] + a[2]) as f64);
289+
}
290+
291+
let check = |y1: f64| -> bool {
292+
let mut t: f64 = 0.0;
293+
for a in squares.iter() {
294+
let y = a[1] as f64;
295+
let l = a[2] as f64;
296+
if y < y1 {
297+
let h = l.min(y1 - y);
298+
t += l * h;
299+
}
300+
}
301+
t >= s / 2.0
302+
};
303+
304+
const EPS: f64 = 1e-5;
305+
while r - l > EPS {
306+
let mid = (l + r) / 2.0;
307+
if check(mid) {
308+
r = mid;
309+
} else {
310+
l = mid;
311+
}
312+
}
313+
r
314+
}
315+
}
316+
```
317+
275318
<!-- tabs:end -->
276319

277320
<!-- solution:end -->
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
impl Solution {
2+
pub fn separate_squares(squares: Vec<Vec<i32>>) -> f64 {
3+
let mut s: f64 = 0.0;
4+
5+
let mut l: f64 = 0.0;
6+
let mut r: f64 = 0.0;
7+
8+
for a in squares.iter() {
9+
let len = a[2] as f64;
10+
s += len * len;
11+
r = r.max((a[1] + a[2]) as f64);
12+
}
13+
14+
let check = |y1: f64| -> bool {
15+
let mut t: f64 = 0.0;
16+
for a in squares.iter() {
17+
let y = a[1] as f64;
18+
let l = a[2] as f64;
19+
if y < y1 {
20+
let h = l.min(y1 - y);
21+
t += l * h;
22+
}
23+
}
24+
t >= s / 2.0
25+
};
26+
27+
const EPS: f64 = 1e-5;
28+
while r - l > EPS {
29+
let mid = (l + r) / 2.0;
30+
if check(mid) {
31+
r = mid;
32+
} else {
33+
l = mid;
34+
}
35+
}
36+
r
37+
}
38+
}

0 commit comments

Comments
 (0)