Skip to content

Commit

Permalink
Merge pull request neetcode-gh#2708 from tahzeer/patch-4
Browse files Browse the repository at this point in the history
Create 2542-maximum-subsequence-score.cpp
  • Loading branch information
felivalencia3 authored Aug 4, 2023
2 parents fd9f4fc + f699658 commit e19c38e
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions cpp/2542-maximum-subsequence-score.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
};

0 comments on commit e19c38e

Please sign in to comment.