-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path347.cpp
64 lines (64 loc) · 1.68 KB
/
347.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
55
56
57
58
59
60
61
62
63
64
// hashmap+bucketsort+29ms.cpp
class Solution {
public:
vector<int> topKFrequent(vector<int> &nums, int k) {
unordered_map<int, int> hashmap;
for (auto num : nums)
hashmap[num]++;
int i, j, n = nums.size();
vector<vector<int>> res(n);
for (auto pairs : hashmap)
res[pairs.second - 1].emplace_back(pairs.first);
vector<int> r(k);
for (i = 0, j = n - 1; i < k;)
if (res[j].size() > 0) {
r[i++] = res[j].back();
res[j].pop_back();
} else
--j;
return r;
}
};
// hashmap+heap+19ms.cpp
class Solution2 {
public:
vector<int> topKFrequent(vector<int> &nums, int k) {
unordered_map<int, int> hashmap;
for (auto num : nums)
hashmap[num]++;
vector<pair<int, int>> res(hashmap.size());
int i = 0;
for (auto pairs : hashmap)
res[i++] = pairs;
vector<int> r(k);
make_heap(res.begin(), res.end(), [](pair<int, int> &a, pair<int, int> &b) {
return a.second < b.second;
});
for (i = 0; i < k;) {
r[i++] = res.front().first;
pop_heap(res.begin(), res.end(),
[](pair<int, int> &a, pair<int, int> &b) {
return a.second < b.second;
});
res.pop_back();
}
return r;
}
};
// hashmap+multimap+22ms.cpp
class Solution3 {
public:
vector<int> topKFrequent(vector<int> &nums, int k) {
unordered_map<int, int> hashmap;
vector<int> res(k);
multimap<int, int> treemap;
for (auto num : nums)
hashmap[num]++;
for (auto pairs : hashmap)
treemap.insert(make_pair(pairs.second, pairs.first));
int i = 0;
for (auto it = treemap.rbegin(); i < k; ++it)
res[i++] = it->second;
return res;
}
};