-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path30_Subsequence_of_Length_K.cpp
More file actions
60 lines (41 loc) · 1.56 KB
/
30_Subsequence_of_Length_K.cpp
File metadata and controls
60 lines (41 loc) · 1.56 KB
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
55
56
57
58
59
60
// You are given an integer array nums and an integer k. You want to find a subsequence of nums of length k that has the largest sum.
// Return any such subsequence as an integer array of length k.
// A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
// Example 1:
// Input: nums = [2,1,3,3], k = 2
// Output: [3,3]
// Explanation:
// The subsequence has the largest sum of 3 + 3 = 6.
// Example 2:
// Input: nums = [-1,-2,3,4], k = 3
// Output: [-1,3,4]
// Explanation:
// The subsequence has the largest sum of -1 + 3 + 4 = 6.
// Example 3:
// Input: nums = [3,4,3,3], k = 2
// Output: [3,4]
// Explanation:
// The subsequence has the largest sum of 3 + 4 = 7.
// Another possible subsequence is [4, 3].
// solution and optimal solution also but chatGPT
class Solution {
public:
vector<int> maxSubsequence(vector<int>& nums, int k) {
priority_queue<pair<int,int>,vector<pair<int,int>>, greater<>> minheap;
for(int i=0;i<nums.size();i++){
minheap.push({nums[i],i});
if(minheap.size()>k) minheap.pop();
}
vector<pair<int,int>> temp;
while(!minheap.empty()){
temp.push_back(minheap.top());
minheap.pop();
}
sort(temp.begin(),temp.end(),[](auto &a,auto &b){
return a.second<b.second;
});
vector<int> ans;
for(auto &p: temp) ans.push_back(p.first);
return ans;
}
};