Given a non-empty array of integers, return the k most frequent elements.
For example,
Given [1,1,1,2,2,3] and k = 2, return [1,2].
Note:
You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
Your algorithm's time complexity must be better than O(n log n), where n is the array's size.
HashMapCount后 的思路类似:215. Kth Largest Element in an Array
http://www.jianshu.com/p/cbeeddb3cd1a
Solution1:HashMapCount + Bucketsort
思路: MapCount: num -> count,将count放到bucket中找出前k个num
Time Complexity: O(N) Space Complexity: O(N)
Solution2:HashMapCount + MaxHeap Sort
思路: MapCount: num -> count,建MaxHeap,将(num,count)放到堆中以count排出前k个num
Time Complexity: O(N + k * logN) N建堆
Space Complexity: O(N)
Solution3:HashMapCount + MinHeap 过滤维护前k个 最多count的元素
思路: MapCount: num -> count,建MaxHeap,将(num,count)放到堆中以count排出前k个num
Time Complexity: O(k + N * logk) k建堆
Space Complexity: O(N)
Solution4:HashMapCount + TreeMap作sort
思路: MapCount: num -> count,建TreeMap,积累 (count -> num_list),因为TreeMap key=count有序 所以就可以选出最后最大的count的k个
Time Complexity: O(N * logN)
Space Complexity: O(N)
TreeMap概念可参考:http://www.jianshu.com/p/e29571903591
Solution1 Code:
class Solution {
public List topKFrequent(int[] nums, int k) {
// count
Map frequencyMap = new HashMap();
for (int n : nums) {
frequencyMap.put(n, frequencyMap.getOrDefault(n, 0) + 1);
}
// bucket sort
List[] bucket = new List[nums.length + 1];
for (int key : frequencyMap.keySet()) {
int frequency = frequencyMap.get(key);
if (bucket[frequency] == null) {
bucket[frequency] = new ArrayList<>();
}
bucket[frequency].add(key);
}
// prepare result
List res = new ArrayList<>();
outerloop:
for (int pos = bucket.length - 1; pos >= 0; pos--) {
if(bucket[pos] == null) continue;
for(int i = 0; i < bucket[pos].size(); i++) {
res.add(bucket[pos].get(i));
if(res.size() == k) break outerloop;
}
}
return res;
}
}
Solution2 Code:
class Solution {
public List topKFrequent(int[] nums, int k) {
// count
Map map = new HashMap();
for (int n : nums) {
map.put(n, map.getOrDefault(n, 0) + 1);
}
// max-heap to sort and get the Top K Frequent Elements
PriorityQueue> maxHeap =
new PriorityQueue<>((a,b)->(b.getValue()-a.getValue()));
for(Map.Entry entry: map.entrySet()){
maxHeap.add(entry);
}
List res = new ArrayList<>();
while(res.size() < k){
Map.Entry entry = maxHeap.poll();
res.add(entry.getKey());
}
return res;
}
}
Solution3 Code:
class Solution {
public List topKFrequent(int[] nums, int k) {
// count
Map map = new HashMap();
for (int n : nums) {
map.put(n, map.getOrDefault(n, 0) + 1);
}
// min-heap to keep the Top K Frequent Elements
PriorityQueue> minHeap =
new PriorityQueue<>((a,b)->(a.getValue() - b.getValue()));
for(Map.Entry entry: map.entrySet()) {
minHeap.add(entry);
if(minHeap.size() > k) {
minHeap.poll();
}
}
List res = new ArrayList<>();
while(minHeap.size() > 0){
Map.Entry entry = minHeap.poll();
res.add(entry.getKey());
}
return res;
}
}
Solution4 Code:
class Solution {
public List topKFrequent(int[] nums, int k) {
// count
Map map = new HashMap();
for (int n : nums) {
map.put(n, map.getOrDefault(n, 0) + 1);
}
TreeMap> freqMap = new TreeMap<>();
for(int num : map.keySet()){
int freq = map.get(num);
if(!freqMap.containsKey(freq)){
freqMap.put(freq, new LinkedList<>());
}
freqMap.get(freq).add(num);
}
List res = new ArrayList<>();
outerloop:
while(true) { //since k is vaild
Map.Entry> entry = freqMap.pollLastEntry();
// if(entry == null) break;
for(int i = 0; i < entry.getValue().size() ; i++) {
res.add(entry.getValue().get(i));
if(res.size() == k) break outerloop;
}
}
return res;
}
}