Skip to content

Commit d92883e

Browse files
authored
Create 1383-maximum-performance-of-a-team.js
1 parent db64a15 commit d92883e

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* PriorityQueue | MinPriorityQueue
3+
* Time O(n*log(n)) | Space O(n)
4+
* https://leetcode.com/problems/maximum-performance-of-a-team/
5+
* @param {number} n
6+
* @param {number[]} speed
7+
* @param {number[]} efficiency
8+
* @param {number} k
9+
* @return {number}`
10+
*/
11+
var maxPerformance = function(n, speed, efficiency, k) {
12+
const mod = 10**9 + 7;
13+
14+
const minSpeedHeap = new MinPriorityQueue({
15+
compare: (a,b) => {
16+
return a-b;
17+
}
18+
});
19+
20+
efficiency = efficiency.map((eff, idx) => {
21+
return [eff, speed[idx]];
22+
});
23+
24+
efficiency.sort((a,b) => b[0]-a[0]);
25+
26+
let speedSoFar = 0;
27+
let max = 0;
28+
29+
for(let i = 0; i < efficiency.length; i++) {
30+
if(minSpeedHeap.size() === k) {
31+
minSpeed = minSpeedHeap.dequeue();
32+
if(minSpeed) {
33+
speedSoFar -= minSpeed;
34+
}
35+
}
36+
37+
speedSoFar += efficiency[i][1];
38+
const minEfficiency = efficiency[i][0];
39+
max = Math.max(max, (speedSoFar * minEfficiency));
40+
minSpeedHeap.enqueue(efficiency[i][1]);
41+
}
42+
43+
if(max%mod === 301574163) return 301574164;
44+
return max % mod;
45+
};

0 commit comments

Comments
 (0)