Skip to content

Commit f2f9a6c

Browse files
committed
add 347. Top K Frequent Elements
1 parent 897166a commit f2f9a6c

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

Heap/347_TopKFrequentElements.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* @Author: [email protected]
3+
* @Last Modified time: 2016-07-09 10:20:41
4+
*/
5+
6+
class Solution {
7+
private:
8+
struct bigger{
9+
bool operator()(pair<int, int> &one, pair<int, int>two){
10+
return one.second > two.second;
11+
}
12+
};
13+
public:
14+
vector<int> topKFrequent(vector<int>& nums, int k) {
15+
/* Use unordered_map and priority_queue(minheap) solution.
16+
*
17+
* Use unordered_map to avoid red-black hash implement, which takes almost O(n*lgn).
18+
* We build a min heap with size k, so the time complexity is O(nlgk).
19+
*/
20+
unordered_map<int, int> num_count;
21+
for(const auto &n: nums){
22+
num_count[n] += 1;
23+
}
24+
25+
priority_queue<pair<int, int>, vector<pair<int, int>>, bigger> frequent_heap;
26+
// Build the min-heap with size k.
27+
for(auto it = num_count.begin(); it != num_count.end(); it++){
28+
if(frequent_heap.size() < k){
29+
frequent_heap.push(*it);
30+
}
31+
else if(it->second >= frequent_heap.top().second){
32+
frequent_heap.pop();
33+
frequent_heap.push(*it);
34+
}
35+
}
36+
37+
vector<int> ans;
38+
while(!frequent_heap.empty()){
39+
auto top = frequent_heap.top();
40+
frequent_heap.pop();
41+
ans.push_back(top.first);
42+
}
43+
return ans;
44+
}
45+
};
46+
47+
48+
class Solution_2 {
49+
public:
50+
vector<int> topKFrequent(vector<int>& nums, int k) {
51+
/* Using stl heap tool.
52+
* According to:
53+
* https://discuss.leetcode.com/topic/46664/36ms-neat-c-solution-using-stl-heap-tool
54+
*/
55+
if (nums.empty()) return {};
56+
unordered_map<int, int> m;
57+
for (auto &n : nums) m[n]++;
58+
59+
vector<pair<int, int>> heap;
60+
for (auto &i : m) heap.push_back({i.second, i.first});
61+
62+
vector<int> result;
63+
make_heap(heap.begin(), heap.end());
64+
while (k--) {
65+
result.push_back(heap.front().second);
66+
pop_heap(heap.begin(), heap.end());
67+
heap.pop_back();
68+
}
69+
return result;
70+
}
71+
};
72+
73+
/*
74+
[1,1,1,2,2,3]
75+
2
76+
[1,1,2,3,3,3,4,4,4,4,1,1,1]
77+
3
78+
*/

Heap/347_TopKFrequentElements.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
# @Last Modified time: 2016-07-09 09:41:55
5+
6+
7+
class Solution(object):
8+
def topKFrequent(self, nums, k):
9+
""" Given a non-empty array of integers, return the k most frequent elements.
10+
11+
heapq.nlargest(n, iterable[, key])
12+
Return a list with the n largest elements from the dataset defined by iterable.
13+
"""
14+
num_count = collections.Counter(nums)
15+
return heapq.nlargest(k, num_count, key=lambda x: num_count[x])
16+
17+
18+
class Solution_2(object):
19+
def topKFrequent(self, nums, k):
20+
''' Use Counter to extract the top k frequent elements
21+
22+
most_common(k) return a list of tuples,
23+
where the first item of the tuple is the element,
24+
and the second item of the tuple is the count
25+
Thus, the built-in zip function could be used to extract
26+
the first item from the tuples
27+
'''
28+
return zip(*collections.Counter(nums).most_common(k))[0]
29+
30+
"""
31+
[1,1,1,2,2,3]
32+
2
33+
[1,1,2,3,3,3,4,4,4,4,1,1,1]
34+
3
35+
"""

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
# [Heap](Heap/)
109109
* 023. [Merge k Sorted Lists](Heap/23_MergeKSortedLists.py)
110110
* 295. [Find Median from Data Stream](Heap/295_FindMedianFromDataStream.py)
111+
* 347. [Top K Frequent Elements](Heap/347_TopKFrequentElements.py)
111112

112113
# [Binary Search](BinarySearch/)
113114

0 commit comments

Comments
 (0)