[Google] 出现次数最多的前K个元素
bob, joe, bob, jane, bob, joe, jack
bob = 3 joe = 2
topN(2) = bob, joe .
interface TopN { void insert(String query); List<String> getTop(int n); }
用map<string, int>来存string在数组v中的下标,v存包含string与出现次数cnt的结构体,每次插入后动态调整v,使其按cnt从大到小有序。找前k个只要直接输出就是。
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 class TopK { 5 private: 6 struct node { 7 string id; 8 int cnt; 9 node(string _id, int _cnt) : id(_id), cnt(_cnt) {} 10 }; 11 12 unordered_map<string, int> mp; 13 vector<node> v; 14 15 public: 16 void insert(string s) { 17 if (mp.find(s) == mp.end()) { 18 v.push_back(node(s, 1)); 19 int idx = v.size() - 1; 20 mp[s] = idx; 21 } else { 22 int idx = mp[s], j; 23 ++v[idx].cnt; 24 node tmp = v[idx]; 25 for (j = idx - 1; j >= 0; --j) { 26 if (v[j].cnt < tmp.cnt) { 27 v[j+1] = v[j]; 28 mp[v[j+1].id] = j + 1; 29 } else { 30 break; 31 } 32 } 33 v[j+1] = tmp; 34 mp[tmp.id] = j+1; 35 } 36 } 37 38 vector<string> topk(int n) { 39 vector<string> res; 40 for (int i = 0; i < n && i < v.size(); ++i) { 41 res.push_back(v[i].id); 42 } 43 return res; 44 } 45 }; 46 47 int main() { 48 TopK tk; 49 vector<string> v; 50 tk.insert("bob"); 51 tk.insert("joe"); 52 tk.insert("bob"); 53 tk.insert("jane"); 54 tk.insert("joe"); 55 tk.insert("bob"); 56 tk.insert("jack"); 57 v = tk.topk(2); 58 for (auto a : v) 59 cout << a << " "; 60 cout << endl; 61 return 0; 62 }