-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathMaximum Subsequence Score.cpp
54 lines (36 loc) · 1.7 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
Solution by Rahul Surana
***********************************************************
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 from nums2.
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.
***********************************************************
*/
class Solution {
public:
static constexpr auto fast_io = [](){ std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL); };
long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {
vector<pair<int,int>> x;
for(int i =0 ; i < nums1.size(); i++) x.push_back({nums1[i],nums2[i]});
sort(x.begin(),x.end(),[&](auto &a, auto &b){ return b.second < a.second; });
priority_queue<int,vector<int>, greater<int>> pq;
long long s = 0;
for(int i = 0; i < k; i++){
s += x[i].first;
pq.push(x[i].first);
}
long long ans = (s*x[k-1].second);
for(int i = k; i < nums1.size(); i++)
{
s += x[i].first - pq.top();
pq.pop();
pq.push(x[i].first);
ans=max(ans,s*x[i].second);
}
return ans;
}
};