Skip to content

Commit 38af912

Browse files
authored
feat: add solutions for lc No.3296 (#5055)
1 parent 652e4a7 commit 38af912

4 files changed

Lines changed: 197 additions & 0 deletions

File tree

solution/3200-3299/3296.Minimum Number of Seconds to Make Mountain Height Zero/README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,75 @@ function minNumberOfSeconds(mountainHeight: number, workerTimes: number[]): numb
252252
}
253253
```
254254

255+
#### Rust
256+
257+
```rust
258+
impl Solution {
259+
pub fn min_number_of_seconds(mountain_height: i32, worker_times: Vec<i32>) -> i64 {
260+
let mut l: i64 = 1;
261+
let mut r: i64 = 10_i64.pow(16);
262+
263+
let check = |t: i64| -> bool {
264+
let mut h: i64 = 0;
265+
for &wt in &worker_times {
266+
let wt = wt as f64;
267+
let t_f = t as f64;
268+
let val = ((t_f * 2.0 / wt + 0.25).sqrt() - 0.5).floor() as i64;
269+
h += val;
270+
if h >= mountain_height as i64 {
271+
return true;
272+
}
273+
}
274+
h >= mountain_height as i64
275+
};
276+
277+
while l < r {
278+
let mid = (l + r) >> 1;
279+
if check(mid) {
280+
r = mid;
281+
} else {
282+
l = mid + 1;
283+
}
284+
}
285+
286+
l
287+
}
288+
}
289+
```
290+
291+
#### C#
292+
293+
```cs
294+
public class Solution {
295+
public long MinNumberOfSeconds(int mountainHeight, int[] workerTimes) {
296+
long l = 1, r = (long)1e16;
297+
298+
bool Check(long t) {
299+
long h = 0;
300+
foreach (int wt in workerTimes) {
301+
long val = (long)(Math.Sqrt(t * 2.0 / wt + 0.25) - 0.5);
302+
h += val;
303+
if (h >= mountainHeight) {
304+
return true;
305+
}
306+
}
307+
return h >= mountainHeight;
308+
}
309+
310+
while (l < r) {
311+
long mid = (l + r) >> 1;
312+
if (Check(mid)) {
313+
r = mid;
314+
} else {
315+
l = mid + 1;
316+
}
317+
}
318+
319+
return l;
320+
}
321+
}
322+
```
323+
255324
<!-- tabs:end -->
256325

257326
<!-- solution:end -->

solution/3200-3299/3296.Minimum Number of Seconds to Make Mountain Height Zero/README_EN.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,75 @@ function minNumberOfSeconds(mountainHeight: number, workerTimes: number[]): numb
248248
}
249249
```
250250

251+
#### Rust
252+
253+
```rust
254+
impl Solution {
255+
pub fn min_number_of_seconds(mountain_height: i32, worker_times: Vec<i32>) -> i64 {
256+
let mut l: i64 = 1;
257+
let mut r: i64 = 10_i64.pow(16);
258+
259+
let check = |t: i64| -> bool {
260+
let mut h: i64 = 0;
261+
for &wt in &worker_times {
262+
let wt = wt as f64;
263+
let t_f = t as f64;
264+
let val = ((t_f * 2.0 / wt + 0.25).sqrt() - 0.5).floor() as i64;
265+
h += val;
266+
if h >= mountain_height as i64 {
267+
return true;
268+
}
269+
}
270+
h >= mountain_height as i64
271+
};
272+
273+
while l < r {
274+
let mid = (l + r) >> 1;
275+
if check(mid) {
276+
r = mid;
277+
} else {
278+
l = mid + 1;
279+
}
280+
}
281+
282+
l
283+
}
284+
}
285+
```
286+
287+
#### C#
288+
289+
```cs
290+
public class Solution {
291+
public long MinNumberOfSeconds(int mountainHeight, int[] workerTimes) {
292+
long l = 1, r = (long)1e16;
293+
294+
bool Check(long t) {
295+
long h = 0;
296+
foreach (int wt in workerTimes) {
297+
long val = (long)(Math.Sqrt(t * 2.0 / wt + 0.25) - 0.5);
298+
h += val;
299+
if (h >= mountainHeight) {
300+
return true;
301+
}
302+
}
303+
return h >= mountainHeight;
304+
}
305+
306+
while (l < r) {
307+
long mid = (l + r) >> 1;
308+
if (Check(mid)) {
309+
r = mid;
310+
} else {
311+
l = mid + 1;
312+
}
313+
}
314+
315+
return l;
316+
}
317+
}
318+
```
319+
251320
<!-- tabs:end -->
252321

253322
<!-- solution:end -->
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
public class Solution {
2+
public long MinNumberOfSeconds(int mountainHeight, int[] workerTimes) {
3+
long l = 1, r = (long)1e16;
4+
5+
bool Check(long t) {
6+
long h = 0;
7+
foreach (int wt in workerTimes) {
8+
long val = (long)(Math.Sqrt(t * 2.0 / wt + 0.25) - 0.5);
9+
h += val;
10+
if (h >= mountainHeight) {
11+
return true;
12+
}
13+
}
14+
return h >= mountainHeight;
15+
}
16+
17+
while (l < r) {
18+
long mid = (l + r) >> 1;
19+
if (Check(mid)) {
20+
r = mid;
21+
} else {
22+
l = mid + 1;
23+
}
24+
}
25+
26+
return l;
27+
}
28+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
impl Solution {
2+
pub fn min_number_of_seconds(mountain_height: i32, worker_times: Vec<i32>) -> i64 {
3+
let mut l: i64 = 1;
4+
let mut r: i64 = 10_i64.pow(16);
5+
6+
let check = |t: i64| -> bool {
7+
let mut h: i64 = 0;
8+
for &wt in &worker_times {
9+
let wt = wt as f64;
10+
let t_f = t as f64;
11+
let val = ((t_f * 2.0 / wt + 0.25).sqrt() - 0.5).floor() as i64;
12+
h += val;
13+
if h >= mountain_height as i64 {
14+
return true;
15+
}
16+
}
17+
h >= mountain_height as i64
18+
};
19+
20+
while l < r {
21+
let mid = (l + r) >> 1;
22+
if check(mid) {
23+
r = mid;
24+
} else {
25+
l = mid + 1;
26+
}
27+
}
28+
29+
l
30+
}
31+
}

0 commit comments

Comments
 (0)