forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2542-maximum-subsequence-score.js
47 lines (39 loc) · 1.04 KB
/
2542-maximum-subsequence-score.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
/**
* MinPriorityQueue | Sorting
* Time O(n*log(n)) | space O(n)
* https://leetcode.com/problems/maximum-subsequence-score/
* @param {number[]} nums1
* @param {number[]} nums2
* @param {number} k
* @return {number}
*/
var maxScore = function(nums1, nums2, k) {
const minQ = new MinPriorityQueue({
compare: (a,b) => {
return a - b;
}
});
let maxScore = 0;
let runningTotal = 0;
let takenElements = 0;
const nums12 = nums1.map((num, idx) => {
return [num, nums2[idx]];
});
nums12.sort((a,b) => {
return b[1] - a[1];
});
for(let i = 0; i < nums12.length; i++) {
const n1 = nums12[i][0];
const n2 = nums12[i][1];
runningTotal += n1;
minQ.enqueue(n1);
if(minQ.size() === k) {
maxScore = Math.max(maxScore, runningTotal * n2);
}
if(minQ.size() > k) {
runningTotal -= minQ.dequeue();
maxScore = Math.max(maxScore, runningTotal * n2);
}
}
return maxScore;
};