Skip to content

Commit f8c58e0

Browse files
committed
Create 1383.最大的团队表现值.js
1 parent 7428fed commit f8c58e0

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

1383.最大的团队表现值.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* @param {number} n
3+
* @param {number[]} speed
4+
* @param {number[]} efficiency
5+
* @param {number} k
6+
* @return {number}
7+
*/
8+
var maxPerformance = function(n, speed, efficiency, k) {
9+
const arr = speed.map((s, i) => [s, efficiency[i]]);
10+
arr.sort((a, b) => b[1] - a[1]);
11+
12+
const heap = [];
13+
function add(n) {
14+
if (heap.length >= k - 1) {
15+
if (heap.length === 0 || n <= heap[0]) {
16+
return n;
17+
}
18+
let r = heap[0];
19+
heap[0] = n;
20+
let i = 0;
21+
while (i < heap.length) {
22+
let next = i;
23+
if (i * 2 + 1 < heap.length && heap[i * 2 + 1] < heap[next]) {
24+
next = i * 2 + 1;
25+
}
26+
if (i * 2 + 2 < heap.length && heap[i * 2 + 2] < heap[next]) {
27+
next = i * 2 + 2;
28+
}
29+
if (i === next) {
30+
break;
31+
} else {
32+
const t = heap[i];
33+
heap[i] = heap[next];
34+
heap[next] = t;
35+
i = next;
36+
}
37+
}
38+
return r;
39+
} else {
40+
heap.push(n);
41+
let i = heap.length - 1;
42+
while (i > 0) {
43+
const p = (i - 1) >> 1;
44+
if (heap[i] < heap[p]) {
45+
const t = heap[i];
46+
heap[i] = heap[p];
47+
heap[p] = t;
48+
i = p;
49+
} else {
50+
break;
51+
}
52+
}
53+
return 0;
54+
}
55+
}
56+
57+
let result = 0n;
58+
let sum = 0n;
59+
for (let i = 0; i < n; i++) {
60+
let e = BigInt(arr[i][1]);
61+
sum += BigInt(arr[i][0]);
62+
if (e * sum > result) {
63+
result = e * sum;
64+
}
65+
sum -= BigInt(add(arr[i][0]));
66+
}
67+
return result % 1000000007n;
68+
};

0 commit comments

Comments
 (0)