forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
_347.java
82 lines (71 loc) · 2.28 KB
/
_347.java
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Queue;
/**
* 347. Top K Frequent Elements
*
* 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.*/
public class _347 {
public static class Solution1 {
/**
* Use buckets to hold numbers of the same frequency
* It's averaged at 30 ms on Leetcode.
*/
public List<Integer> topKFrequent(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap();
for (int i : nums) {
map.put(i, map.getOrDefault(i, 0) + 1);
}
ArrayList[] bucket = new ArrayList[nums.length + 1];
for (Entry<Integer, Integer> e : map.entrySet()) {
int frequency = e.getValue();
if (bucket[frequency] == null) {
bucket[frequency] = new ArrayList<Integer>();
}
bucket[frequency].add(e.getKey());
}
List<Integer> result = new ArrayList<>();
for (int i = bucket.length - 1; i >= 0 && result.size() < k; i--) {
if (bucket[i] != null) {
for (int j = 0; j < bucket[i].size(); j++) {
result.add((int) bucket[i].get(j));
}
}
}
return result;
}
}
public static class Solution2 {
/**
* Use hashtable and heap, it's averaged at 100 ms on Leetocde.
*/
public List<Integer> topKFrequent(int[] nums, int k) {
// construct the frequency map first, and then iterate through the map
// and put them into the heap, this is O(n)
Map<Integer, Integer> map = new HashMap();
for (int num : nums) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
// build heap, this is O(logn)
Queue<Entry<Integer, Integer>> heap = new PriorityQueue<>((o1, o2) -> o2.getValue() - o1.getValue());
for (Entry<Integer, Integer> entry : map.entrySet()) {
heap.offer(entry);
}
List<Integer> res = new ArrayList();
while (k-- > 0) {
res.add(heap.poll().getKey());
}
return res;
}
}
}