-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1383.最大的团队表现值.js
68 lines (66 loc) · 1.86 KB
/
1383.最大的团队表现值.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
/**
* @param {number} n
* @param {number[]} speed
* @param {number[]} efficiency
* @param {number} k
* @return {number}
*/
var maxPerformance = function(n, speed, efficiency, k) {
const arr = speed.map((s, i) => [s, efficiency[i]]);
arr.sort((a, b) => b[1] - a[1]);
const heap = [];
function add(n) {
if (heap.length >= k - 1) {
if (heap.length === 0 || n <= heap[0]) {
return n;
}
let r = heap[0];
heap[0] = n;
let i = 0;
while (i < heap.length) {
let next = i;
if (i * 2 + 1 < heap.length && heap[i * 2 + 1] < heap[next]) {
next = i * 2 + 1;
}
if (i * 2 + 2 < heap.length && heap[i * 2 + 2] < heap[next]) {
next = i * 2 + 2;
}
if (i === next) {
break;
} else {
const t = heap[i];
heap[i] = heap[next];
heap[next] = t;
i = next;
}
}
return r;
} else {
heap.push(n);
let i = heap.length - 1;
while (i > 0) {
const p = (i - 1) >> 1;
if (heap[i] < heap[p]) {
const t = heap[i];
heap[i] = heap[p];
heap[p] = t;
i = p;
} else {
break;
}
}
return 0;
}
}
let result = 0n;
let sum = 0n;
for (let i = 0; i < n; i++) {
let e = BigInt(arr[i][1]);
sum += BigInt(arr[i][0]);
if (e * sum > result) {
result = e * sum;
}
sum -= BigInt(add(arr[i][0]));
}
return result % 1000000007n;
};