-
Notifications
You must be signed in to change notification settings - Fork 3
/
692.cpp
60 lines (58 loc) · 1.86 KB
/
692.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
typedef pair<int, string> P;
class Solution {
public:
static bool compare(const P& p1, const P& p2) {
if (p1.first != p2.first) return p1.first > p2.first;
int minLen = min(p1.second.size(), p2.second.size());
for (int i = 0; i < minLen; i++) {
if (p1.second[i] != p2.second[i]) return p1.second[i] < p2.second[i];
}
return p1.second.size() < p2.second.size();
}
vector<string> topKFrequent(vector<string>& words, int k) {
unordered_map<string, int> st;
for (auto word : words) st[word]++;
vector<P> v; // freq, string
for (auto element : st) v.push_back(make_pair(element.second, element.first));
sort(v.begin(), v.end(), compare);
vector<string> ans;
for (int i = 0; i < k; i++) {
ans.push_back(v[i].second);
}
return ans;
}
};
// v2
typedef pair<int, string> P;
class Compare {
public:
bool operator() (const P& p1, const P& p2) const {
if (p1.first == p2.first) return p1.second > p2.second;
return p1.first < p2.first;
}
};
class Solution {
public:
vector<string> topKFrequent(vector<string>& words, int k) {
multiset<P, Compare> mst;
unordered_map<string, int> mp;
for (auto& word : words) {
if (mp.find(word) != mp.end()) {
const pair<int, string> p = {mp[word], word};
if (mst.find(p) != mst.end()) {
mst.erase(mst.find(p));
}
}
mp[word]++;
mst.insert({mp[word], word});
while (mst.size() > k) mst.erase(mst.begin());
}
vector<string> res;
while (!mst.empty()) {
res.push_back(mst.begin()->second);
mst.erase(mst.begin());
}
reverse(res.begin(), res.end());
return res;
}
};