Skip to content

Commit a422d87

Browse files
authored
Create 3296-minimum-number-of-seconds-to-make-mountain-height-zero.js
1 parent d121bba commit a422d87

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {number} mountainHeight
3+
* @param {number[]} workerTimes
4+
* @return {number}
5+
*/
6+
var minNumberOfSeconds = function (mountainHeight, workerTimes) {
7+
let low = 0,
8+
high = 1e18
9+
while (low < high) {
10+
let mid = low + Math.floor((high - low) / 2)
11+
let totalHeightReduced = 0
12+
for (let workerTime of workerTimes) {
13+
totalHeightReduced += f(mid, workerTime)
14+
if (totalHeightReduced >= mountainHeight) break
15+
}
16+
if (totalHeightReduced >= mountainHeight) high = mid
17+
else low = mid + 1
18+
}
19+
return low
20+
}
21+
function f(T, workerTime) {
22+
let low = 0,
23+
high = 1e6
24+
while (low < high) {
25+
let mid = low + Math.floor((high - low + 1) / 2)
26+
let timeRequired = (workerTime * mid * (mid + 1)) / 2
27+
if (timeRequired <= T) low = mid
28+
else high = mid - 1
29+
}
30+
return low
31+
}

0 commit comments

Comments
 (0)