-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathW241013Q1.cpp
49 lines (42 loc) · 1.07 KB
/
W241013Q1.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
#include "leetcode.hpp"
struct helper {
int value;
int count;
bool operator<(const helper& b) {
return count < b.count || (count == b.count && value < b.value);
}
};
class Solution {
public:
vector<int> findXSum(vector<int>& nums, int k, int x) {
vector<int> ret;
const int n = nums.size();
for (int i = 0; i < n - k + 1; ++i) {
map<int, int> hash;
for (int j = i; j < i + k; ++j) {
hash[nums[j]]++;
}
vector<helper> v;
for (auto p : hash) {
v.push_back({p.first, p.second});
}
sort(v.begin(), v.end());
int sum = 0;
for (int j = 0; j < x && j < v.size(); ++j) {
sum += v[v.size() - j - 1].value * v[v.size() - j - 1].count;
}
ret.push_back(sum);
}
return ret;
}
};
int main(int argc, char const* argv[]) {
Solution s;
vector<int> t1 = {1, 1, 2, 2, 3, 4, 2, 3};
vector<int> t2 = {3, 8, 7, 8, 7, 5};
vector<int> t3 = {9, 2, 2};
// auto r1 = s.findXSum(t1, 6, 2);
// auto r2 = s.findXSum(t2, 2, 2);
auto r3 = s.findXSum(t3, 3, 3);
return 0;
}