-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy path2141-maximum-running-time-of-n-computers.js
69 lines (61 loc) · 1.3 KB
/
2141-maximum-running-time-of-n-computers.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* @param {number} n
* @param {number[]} batteries
* @return {number}
*/
const maxRunTime = function(n, batteries) {
n = BigInt(n)
batteries = batteries.map(e => BigInt(e))
const sum = batteries.reduce((ac, e) => ac + e, 0n)
let l = 0n, r = sum / n
while(l < r) {
const mid = r - (r - l) / 2n
if(valid(mid)) l = mid
else r = mid - 1n
}
return l
function valid(mid) {
let curSum = 0n, target = mid * n
for(const e of batteries) {
curSum += e > mid ? mid : e
if(curSum >= target) return true
}
return false
}
};
// another
/**
* @param {number} n
* @param {number[]} batteries
* @return {number}
*/
var maxRunTime = function (n, batteries) {
batteries.sort((a, b) => a - b)
const sum = batteries.reduce((ac, e) => ac + BigInt(e), 0n)
let hi = ~~(sum / BigInt(n)) + 1n,
lo = 0n
while (lo < hi) {
let mid = ~~((lo + hi) / 2n)
if (chk(mid)) {
lo = mid + 1n
} else {
hi = mid
}
}
return lo - 1n
function chk(x) {
let current = 0n
let i = 0n
for (let b of batteries) {
if (i == BigInt(n)) break
if (b > x) b = x
if (b >= x - current) {
i += 1n
current = BigInt(b) - (x - current)
} else {
current += BigInt(b)
}
}
return i == n
}
}