|
8 | 8 | import java.util.PriorityQueue;
|
9 | 9 | import java.util.Queue;
|
10 | 10 |
|
11 |
| -/** |
12 |
| - * 347. Top K Frequent Elements |
13 |
| - * |
14 |
| - * Given a non-empty array of integers, return the k most frequent elements. |
15 |
| -
|
16 |
| - For example, |
17 |
| - Given [1,1,1,2,2,3] and k = 2, return [1,2]. |
18 |
| -
|
19 |
| - Note: |
20 |
| - You may assume k is always valid, 1 ≤ k ≤ number of unique elements. |
21 |
| - Your algorithm's time complexity must be better than O(n log n), where n is the array's size.*/ |
22 |
| - |
23 | 11 | public class _347 {
|
24 | 12 |
|
25 |
| - public static class Solution1 { |
26 |
| - /** |
27 |
| - * Use buckets to hold numbers of the same frequency |
28 |
| - * It's averaged at 30 ms on Leetcode. |
29 |
| - */ |
30 |
| - public List<Integer> topKFrequent(int[] nums, int k) { |
31 |
| - Map<Integer, Integer> map = new HashMap(); |
32 |
| - for (int i : nums) { |
33 |
| - map.put(i, map.getOrDefault(i, 0) + 1); |
34 |
| - } |
| 13 | + public static class Solution1 { |
| 14 | + /** |
| 15 | + * Use buckets to hold numbers of the same frequency |
| 16 | + * It's averaged at 30 ms on Leetcode. |
| 17 | + */ |
| 18 | + public List<Integer> topKFrequent(int[] nums, int k) { |
| 19 | + Map<Integer, Integer> map = new HashMap(); |
| 20 | + for (int i : nums) { |
| 21 | + map.put(i, map.getOrDefault(i, 0) + 1); |
| 22 | + } |
35 | 23 |
|
36 |
| - ArrayList[] bucket = new ArrayList[nums.length + 1]; |
37 |
| - for (Entry<Integer, Integer> e : map.entrySet()) { |
38 |
| - int frequency = e.getValue(); |
39 |
| - if (bucket[frequency] == null) { |
40 |
| - bucket[frequency] = new ArrayList<Integer>(); |
41 |
| - } |
42 |
| - bucket[frequency].add(e.getKey()); |
43 |
| - } |
44 |
| - List<Integer> result = new ArrayList<>(); |
45 |
| - for (int i = bucket.length - 1; i >= 0 && result.size() < k; i--) { |
46 |
| - if (bucket[i] != null) { |
47 |
| - for (int j = 0; j < bucket[i].size(); j++) { |
48 |
| - result.add((int) bucket[i].get(j)); |
49 |
| - } |
50 |
| - } |
51 |
| - } |
| 24 | + ArrayList[] bucket = new ArrayList[nums.length + 1]; |
| 25 | + for (Entry<Integer, Integer> e : map.entrySet()) { |
| 26 | + int frequency = e.getValue(); |
| 27 | + if (bucket[frequency] == null) { |
| 28 | + bucket[frequency] = new ArrayList<Integer>(); |
| 29 | + } |
| 30 | + bucket[frequency].add(e.getKey()); |
| 31 | + } |
| 32 | + List<Integer> result = new ArrayList<>(); |
| 33 | + for (int i = bucket.length - 1; i >= 0 && result.size() < k; i--) { |
| 34 | + if (bucket[i] != null) { |
| 35 | + for (int j = 0; j < bucket[i].size(); j++) { |
| 36 | + result.add((int) bucket[i].get(j)); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
52 | 40 |
|
53 |
| - return result; |
54 |
| - } |
55 |
| - } |
| 41 | + return result; |
| 42 | + } |
| 43 | + } |
56 | 44 |
|
57 |
| - public static class Solution2 { |
58 |
| - /** |
59 |
| - * Use hashtable and heap, it's averaged at 100 ms on Leetocde. |
60 |
| - */ |
61 |
| - public List<Integer> topKFrequent(int[] nums, int k) { |
62 |
| - // construct the frequency map first, and then iterate through the map |
63 |
| - // and put them into the heap, this is O(n) |
64 |
| - Map<Integer, Integer> map = new HashMap(); |
65 |
| - for (int num : nums) { |
66 |
| - map.put(num, map.getOrDefault(num, 0) + 1); |
67 |
| - } |
| 45 | + public static class Solution2 { |
| 46 | + /** |
| 47 | + * Use hashtable and heap, it's averaged at 100 ms on Leetocde. |
| 48 | + */ |
| 49 | + public List<Integer> topKFrequent(int[] nums, int k) { |
| 50 | + // construct the frequency map first, and then iterate through the map |
| 51 | + // and put them into the heap, this is O(n) |
| 52 | + Map<Integer, Integer> map = new HashMap(); |
| 53 | + for (int num : nums) { |
| 54 | + map.put(num, map.getOrDefault(num, 0) + 1); |
| 55 | + } |
68 | 56 |
|
69 |
| - // build heap, this is O(logn) |
70 |
| - Queue<Entry<Integer, Integer>> heap = new PriorityQueue<>((o1, o2) -> o2.getValue() - o1.getValue()); |
71 |
| - for (Entry<Integer, Integer> entry : map.entrySet()) { |
72 |
| - heap.offer(entry); |
73 |
| - } |
| 57 | + // build heap, this is O(logn) |
| 58 | + Queue<Entry<Integer, Integer>> heap = new PriorityQueue<>((o1, o2) -> o2.getValue() - o1.getValue()); |
| 59 | + for (Entry<Integer, Integer> entry : map.entrySet()) { |
| 60 | + heap.offer(entry); |
| 61 | + } |
74 | 62 |
|
75 |
| - List<Integer> res = new ArrayList(); |
76 |
| - while (k-- > 0) { |
77 |
| - res.add(heap.poll().getKey()); |
78 |
| - } |
79 |
| - return res; |
80 |
| - } |
81 |
| - } |
| 63 | + List<Integer> res = new ArrayList(); |
| 64 | + while (k-- > 0) { |
| 65 | + res.add(heap.poll().getKey()); |
| 66 | + } |
| 67 | + return res; |
| 68 | + } |
| 69 | + } |
82 | 70 | }
|
0 commit comments