2023-05-24 Daily Challenge
Today I have done leetcode's May LeetCoding Challenge with cpp
.
May LeetCoding Challenge 24
Description
Maximum Subsequence Score
You are given two 0-indexed integer arrays nums1
and nums2
of equal length n
and a positive integer k
. You must choose a subsequence of indices from nums1
of length k
.
For chosen indices i0
, i1
, ..., ik - 1
, your score is defined as:
- The sum of the selected elements from
nums1
multiplied with the minimum of the selected elements fromnums2
. - It can defined simply as:
(nums1[i0] + nums1[i1] +...+ nums1[ik - 1]) * min(nums2[i0] , nums2[i1], ... ,nums2[ik - 1])
.
Return the maximum possible score.
A subsequence of indices of an array is a set that can be derived from the set {0, 1, ..., n-1}
by deleting some or no elements.
Example 1:
Input: nums1 = [1,3,3,2], nums2 = [2,1,3,4], k = 3 Output: 12 Explanation: The four possible subsequence scores are: - We choose the indices 0, 1, and 2 with score = (1+3+3) * min(2,1,3) = 7. - We choose the indices 0, 1, and 3 with score = (1+3+2) * min(2,1,4) = 6. - We choose the indices 0, 2, and 3 with score = (1+3+2) * min(2,3,4) = 12. - We choose the indices 1, 2, and 3 with score = (3+3+2) * min(1,3,4) = 8. Therefore, we return the max score, which is 12.
Example 2:
Input: nums1 = [4,2,3,1,1], nums2 = [7,5,10,9,6], k = 1 Output: 30 Explanation: Choosing index 2 is optimal: nums1[2] * nums2[2] = 3 * 10 = 30 is the maximum possible score.
Constraints:
n == nums1.length == nums2.length
1 <= n <= 105
0 <= nums1[i], nums2[j] <= 105
1 <= k <= n
Solution
auto speedup = [](){
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
return 0;
}();
class Solution {
using pi = pair<int, int>;
public:
long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {
vector<pi> pairs;
pairs.reserve(nums1.size());
for(int i = 0; i < nums1.size(); ++i) {
pairs.push_back({nums2[i], nums1[i]});
}
sort(pairs.begin(), pairs.end(), greater<pi>());
priority_queue<int, vector<int>, greater<int>> pq;
long long sum = 0;
int count = 0;
long long answer = 0;
for(const auto &[factor, num] : pairs) {
pq.push(num);
count += 1;
sum += num;
if(count > k) {
sum -= pq.top();
pq.pop();
count -= 1;
}
if(count == k) {
answer = max(answer, sum * factor);
}
}
return answer;
}
};
// Accepted
// 28/28 cases passed (205 ms)
// Your runtime beats 100 % of cpp submissions
// Your memory usage beats 95.07 % of cpp submissions (87.2 MB)