-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy path2141-maximum-running-time-of-n-computers.js
More file actions
41 lines (37 loc) · 1.38 KB
/
2141-maximum-running-time-of-n-computers.js
File metadata and controls
41 lines (37 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* 2141. Maximum Running Time of N Computers
* https://leetcode.com/problems/maximum-running-time-of-n-computers/
* Difficulty: Hard
*
* 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 all n 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 the maximum number of minutes you can run all the n computers simultaneously.
*/
/**
* @param {number} n
* @param {number[]} batteries
* @return {number}
*/
var maxRunTime = function(n, batteries) {
let left = 1;
let right = Math.floor(batteries.reduce((sum, battery) => sum + battery, 0) / n);
while (left < right) {
const mid = Math.floor((left + right + 1) / 2);
const total = batteries.reduce((sum, battery) => sum + Math.min(battery, mid), 0);
if (total >= n * mid) {
left = mid;
} else {
right = mid - 1;
}
}
return left;
};