You have n computers. You are given the integer n and a 0-indexed integer array batteries where the ith battery can run a computer for
batteries[i] minutes. You are interested in running alln computers
simultaneously using the given batteries.
Initially, you can insert at most one battery into each computer. After that and at any integer time moment, you can remove a battery from a computer and insert another battery any number of times. The inserted battery can be a totally new battery or a battery from another computer. You may assume that the removing and inserting processes take no time.
Note that the batteries cannot be recharged.
Return _themaximum number of minutes you can run all the _ncomputers simultaneously.

Input: n =2, batteries =[3,3,3]Output: 4Explanation:
Initially, insert battery 0 into the first computer and battery 1 into the second computer.After two minutes, remove battery 1 from the second computer and insert battery 2 instead. Note that battery 1 can still run for one minute.At the end of the third minute, battery 0is drained, and you need to remove it from the first computer and insert battery 1 instead.By the end of the fourth minute, battery 1is also drained, and the first computer is no longer running.We can run the two computers simultaneously for at most 4 minutes, so we return4.

Input: n =2, batteries =[1,1,1,1]Output: 2Explanation:
Initially, insert battery 0 into the first computer and battery 2 into the second computer.After one minute, battery 0 and battery 2 are drained so you need to remove them and insert battery 1 into the first computer and battery 3 into the second computer.After another minute, battery 1 and battery 3 are also drained so the first and second computers are no longer running.We can run the two computers simultaneously for at most 2 minutes, so we return2.
We want to maximize the time all n computers can run simultaneously using the given batteries. The key is that batteries can be swapped at any time, so the total available energy can be distributed optimally. The answer is the largest integer T such that the sum of the min(battery, T) for all batteries is at least n * T.
classSolution {
public:longlong maxRunTime(int n, vector<int>& batteries) {
longlong left =1, right = accumulate(batteries.begin(), batteries.end(), 0LL) / n, ans =0;
while (left <= right) {
longlong mid = left + (right - left) /2, total =0;
for (int b : batteries) total += min((longlong)b, mid);
if (total >= n * mid) {
ans = mid;
left = mid +1;
} else {
right = mid -1;
}
}
return ans;
}
};
classSolution {
publiclongmaxRunTime(int n, int[] batteries) {
long sum = 0;
for (int b : batteries) sum += b;
long left = 1, right = sum / n, ans = 0;
while (left <= right) {
long mid = left + (right - left) / 2, total = 0;
for (int b : batteries) total += Math.min(b, mid);
if (total >= n * mid) {
ans = mid;
left = mid + 1;
} else {
right = mid - 1;
}
}
return ans;
}
}
classSolution {
funmaxRunTime(n: Int, batteries: IntArray): Long {
var sum = batteries.map { it.toLong() }.sum()
var left = 1Lvar right = sum / n
var ans = 0Lwhile (left <= right) {
val mid = left + (right - left) / 2var total = 0Lfor (b in batteries) total += minOf(b.toLong(), mid)
if (total >= n * mid) {
ans = mid
left = mid + 1 } else {
right = mid - 1 }
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution:
defmaxRunTime(self, n: int, batteries: list[int]) -> int:
total = sum(batteries)
left, right, ans =1, total // n, 0while left <= right:
mid = (left + right) //2 s = sum(min(b, mid) for b in batteries)
if s >= n * mid:
ans = mid
left = mid +1else:
right = mid -1return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
impl Solution {
pubfnmax_run_time(n: i32, batteries: Vec<i32>) -> i64 {
let sum: i64= batteries.iter().map(|&b| b asi64).sum();
let (mut left, mut right, mut ans) = (1, sum / n asi64, 0);
while left <= right {
let mid = left + (right - left) /2;
let total: i64= batteries.iter().map(|&b| std::cmp::min(b asi64, mid)).sum();
if total >= n asi64* mid {
ans = mid;
left = mid +1;
} else {
right = mid -1;
}
}
ans
}
}