-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2099.cpp
More file actions
54 lines (40 loc) · 1.06 KB
/
2099.cpp
File metadata and controls
54 lines (40 loc) · 1.06 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
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
std::vector<int> maxSubsequence(std::vector<int>& nums, int k) {
std::vector<int> vec;
std::map<int, std::vector<int>> valueToIndices;
for(int i = 0; i < nums.size(); i++) {
valueToIndices[nums[i]].push_back(i);
}
for(const auto& i : valueToIndices) {
std::cout << i.first << " ";
}
int a = 1;
int i = 0;
for(auto it = valueToIndices.rbegin(); it != valueToIndices.rend(); ++it) {
const auto& indices = it->second;
for(int index : indices) {
vec.push_back(nums[index]);
++i;
++a;
}
if(a > k) {
break;
}
}
std::reverse(vec.begin(), vec.end());
return vec;
}
int main() {
// std::vector<int> nums = { 2,1,3,3 };
// std::vector<int> nums = { -1,-2,3,4 };
std::vector<int> nums = { 50,-75 };
int k = 2;
std::vector<int> vec = maxSubsequence(nums, k);
for(int i : vec) {
std::cout << i << " ";
}
std::cin.get();
}