forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main3.cpp
66 lines (53 loc) · 1.7 KB
/
main3.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
65
66
/// Source : https://leetcode.com/problems/top-k-frequent-elements/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
#include <cassert>
using namespace std;
/// Priority Queue
/// Time Complexity: O(nlogn)
/// Space Complexity: O(n)
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
assert(k > 0);
// 统计每个元素出现的频率
unordered_map<int,int> freq;
for(int i = 0 ; i < nums.size() ; i ++ )
freq[nums[i]] ++;
assert(k <= freq.size());
// 扫描freq,维护当前出现频率最高的k个元素
// 在优先队列中,按照频率排序,所以数据对是 (频率,元素) 的形式
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
for(unordered_map<int,int>::iterator iter = freq.begin();
iter != freq.end(); iter ++ ){
if(pq.size() == k){
if(iter->second > pq.top().first){
pq.pop();
pq.push( make_pair(iter->second, iter->first));
}
}
else
pq.push(make_pair(iter->second , iter->first));
}
vector<int> res;
while(!pq.empty()){
res.push_back(pq.top().second);
pq.pop();
}
return res;
}
};
int main() {
int nums[] = {1, 1, 1, 2, 2, 3};
vector<int> vec(nums, nums + sizeof(nums)/sizeof(int));
int k = 2;
vector<int> res = Solution().topKFrequent(vec, 2);
for( int i = 0 ; i < res.size() ; i ++ )
cout<<res[i]<<" ";
cout<<endl;
return 0;
}