Input: mountainHeight =4, workerTimes =[2,1,1]Output: 3Explanation:
One way the height of the mountain can be reduced to 0is:* Worker 0 reduces the height by 1, taking `workerTimes[0] = 2` seconds.* Worker 1 reduces the height by 2, taking `workerTimes[1] + workerTimes[1] * 2 = 3` seconds.* Worker 2 reduces the height by 1, taking `workerTimes[2] = 1` second.Since they work simultaneously, the minimum time needed is`max(2, 3, 1) = 3`seconds.
Input: mountainHeight =5, workerTimes =[1]Output: 15Explanation:
There is only one worker inthis example, so the answer is`workerTimes[0] +
workerTimes[0] * 2 + workerTimes[0] * 3 + workerTimes[0] * 4 + workerTimes[0]
* 5 = 15`.
Each worker can reduce some part of the mountain, and the time for each is the sum of an arithmetic progression. We want to partition the mountainHeight among workers so that the maximum time among all workers is minimized. This is a classic load balancing problem solved by binary search.
#include<vector>#include<algorithm>usingnamespace std;
classSolution {
public:int minSeconds(int mountainHeight, vector<int>& workerTimes) {
int left =1, right =1e15, ans = right;
while (left <= right) {
longlong mid = left + (right - left) /2;
longlong total =0;
for (int wt : workerTimes) {
longlong l =0, r = mountainHeight;
while (l < r) {
longlong m = (l + r +1) /2;
longlong t = wt * m * (m +1) /2;
if (t <= mid) l = m;
else r = m -1;
}
total += l;
}
if (total >= mountainHeight) {
ans = mid;
right = mid -1;
} else {
left = mid +1;
}
}
return ans;
}
};
classSolution {
publiclongminSeconds(int mountainHeight, int[] workerTimes) {
long left = 1, right = (long)1e15, ans = right;
while (left <= right) {
long mid = left + (right - left) / 2;
long total = 0;
for (int wt : workerTimes) {
long l = 0, r = mountainHeight;
while (l < r) {
long m = (l + r + 1) / 2;
long t = wt * m * (m + 1) / 2;
if (t <= mid) l = m;
else r = m - 1;
}
total += l;
}
if (total >= mountainHeight) {
ans = mid;
right = mid - 1;
} else {
left = mid + 1;
}
}
return ans;
}
}
classSolution {
funminSeconds(mountainHeight: Int, workerTimes: IntArray): Long {
var left = 1L; var right = 1_000_000_000_000_000L; var ans = right
while (left <= right) {
val mid = (left + right) / 2var total = 0Lfor (wt in workerTimes) {
var l = 0L; var r = mountainHeight.toLong()
while (l < r) {
val m = (l + r + 1) / 2val t = wt * m * (m + 1) / 2if (t <= mid) l = m else r = m - 1 }
total += l
}
if (total >= mountainHeight) {
ans = mid
right = mid - 1 } else {
left = mid + 1 }
}
return ans
}
}
classSolution:
defminSeconds(self, mountainHeight: int, workerTimes: list[int]) -> int:
left, right =1, 10**15 ans = right
while left <= right:
mid = (left + right) //2 total =0for wt in workerTimes:
l, r =0, mountainHeight
while l < r:
m = (l + r +1) //2 t = wt * m * (m +1) //2if t <= mid:
l = m
else:
r = m -1 total += l
if total >= mountainHeight:
ans = mid
right = mid -1else:
left = mid +1return ans
impl Solution {
pubfnmin_seconds(mountain_height: i32, worker_times: Vec<i32>) -> i64 {
let (mut left, mut right) = (1i64, 1e15asi64);
letmut ans = right;
while left <= right {
let mid = (left + right) /2;
letmut total =0i64;
for&wt in&worker_times {
let (mut l, mut r) = (0i64, mountain_height asi64);
while l < r {
let m = (l + r +1) /2;
let t = wt asi64* m * (m +1) /2;
if t <= mid { l = m; } else { r = m -1; }
}
total += l;
}
if total >= mountain_height asi64 {
ans = mid;
right = mid -1;
} else {
left = mid +1;
}
}
ans
}
}