forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2542-maximum-subsequence-score.cpp
38 lines (32 loc) · 1.05 KB
/
2542-maximum-subsequence-score.cpp
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
// Time: O(NlogN)
// Space: O(N)
class Solution {
public:
long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {
int size = nums1.size();
vector<pair<int, int>> pairs(size);
// populating the array
for(int i = 0; i < size; i++) {
pairs.push_back(make_pair(nums1[i], nums2[i]));
}
// sorting the array using comparator lambda function
sort(pairs.begin(), pairs.end(), [](pair<int, int> a, pair<int, int> b) {
return (a.second > b.second);
});
priority_queue<int, vector<int>, greater<int>> minh;
long long currSum = 0;
long long maxSum = INT_MIN;
for(int i = 0; i < size; i++) {
currSum += pairs[i].first;
minh.push(pairs[i].first);
if(minh.size() > k) {
currSum -= minh.top();
minh.pop();
}
if(minh.size() == k) {
maxSum = max(maxSum, (currSum * pairs[i].second));
}
}
return maxSum;
}
};