-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsolution1.js
75 lines (68 loc) · 1.73 KB
/
solution1.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
70
71
72
73
74
75
/**
* https://leetcode-cn.com/problems/maximum-performance-of-a-team/
*
* 5359. 最大的团队表现值
*
* Hard
*
* 1、堆 实现优先队列
* 2、BigInt
*
* 360ms 100.00%;
* 60.4mb 100.00%
*/
const maxPerformance = (n, speed, efficiency, k) => {
let list = efficiency.map((item, index) => [item, speed[index]]);
list.sort((a, b) => b[0] - a[0]);
let pq = new PriorityQueue();
let sum = 0;
let ans = 0;
for (let i = 0; i < n; i++) {
const currentEfficiency = list[i][0];
if (pq.heap.length === k) {
sum -= pq.pop_first();
}
sum = BigInt(sum) + BigInt(list[i][1]);
pq.push_first(BigInt(list[i][1]));
const temp = BigInt(currentEfficiency) * BigInt(sum);
if (temp > ans) {
ans = temp;
}
}
return ans % 1000000007n;
}
function PriorityQueue() {
this.heap = [];
}
PriorityQueue.prototype.heapify = function (arr, n, index, isDown) {
if (index >= n) {
return;
}
const c1 = index * 2 + 1;
const c2 = index * 2 + 2;
let minIndex = index;
if (c1 < n && arr[c1] < arr[minIndex]) {
minIndex = c1;
}
if (c2 < n && arr[c2] < arr[minIndex]) {
minIndex = c2;
}
if (minIndex !== index) {
[arr[minIndex], arr[index]] = [arr[index], arr[minIndex]];
const nextIndex = isDown ? minIndex : Math.floor((index - 1) / 2);
this.heapify(arr, n, nextIndex, isDown);
}
}
PriorityQueue.prototype.pop_first = function () {
const min = this.heap[0];
const n = this.heap.length;
this.heap[0] = this.heap[n - 1];
this.heap.pop();
this.heapify(this.heap, n, 0, true);
return min;
}
PriorityQueue.prototype.push_first = function (value) {
this.heap.push(value);
const n = this.heap.length;
this.heapify(this.heap, n, Math.floor((n - 2) / 2), false);
}