|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 | 3 | import java.util.ArrayList;
|
4 |
| -import java.util.Comparator; |
5 | 4 | import java.util.HashMap;
|
6 | 5 | import java.util.List;
|
7 | 6 | import java.util.Map;
|
|
22 | 21 | Your algorithm's time complexity must be better than O(n log n), where n is the array's size.*/
|
23 | 22 |
|
24 | 23 | public class _347 {
|
25 |
| - // Approach 1: use buckets to hold numbers of the same frequency |
26 |
| - /**Attn: we must use a simple array to solve this problem, instead of using List<List<Integer>>, |
27 |
| - * we have to use List<Integer>[], otherwise, cases like this one: [-1,-1] |
28 |
| - * 1 will fail due to the fact that ArrayList.get(i), |
29 |
| - * this i must be a non-negative number, however, in simple arrays, the index could be negative. |
30 |
| - * Although in this question, frequency will be at least 1, but still in problems like this where bucket sort |
31 |
| - * works the best, you should use List<Integer>[], this will simplify the code.*/ |
32 |
| - public List<Integer> topKFrequent_using_bucket(int[] nums, int k) { |
33 |
| - Map<Integer, Integer> map = new HashMap(); |
34 |
| - for (int i : nums) { |
35 |
| - map.put(i, map.getOrDefault(i, 0) + 1); |
36 |
| - } |
37 | 24 |
|
38 |
| - ArrayList[] bucket = new ArrayList[nums.length + 1]; |
39 |
| - for (Entry<Integer, Integer> e : map.entrySet()) { |
40 |
| - int frequency = e.getValue(); |
41 |
| - if (bucket[frequency] == null) { |
42 |
| - bucket[frequency] = new ArrayList<Integer>(); |
| 25 | + public static class Solution1 { |
| 26 | + /** |
| 27 | + * Use buckets to hold numbers of the same frequency |
| 28 | + */ |
| 29 | + public List<Integer> topKFrequent(int[] nums, int k) { |
| 30 | + Map<Integer, Integer> map = new HashMap(); |
| 31 | + for (int i : nums) { |
| 32 | + map.put(i, map.getOrDefault(i, 0) + 1); |
43 | 33 | }
|
44 |
| - bucket[frequency].add(e.getKey()); |
45 |
| - } |
46 |
| - List<Integer> result = new ArrayList<Integer>(); |
47 |
| - for (int i = bucket.length - 1; i >= 0 && result.size() < k; i--) { |
48 |
| - if (bucket[i] != null) { |
49 |
| - result.addAll(bucket[i]); |
| 34 | + |
| 35 | + ArrayList[] bucket = new ArrayList[nums.length + 1]; |
| 36 | + for (Entry<Integer, Integer> e : map.entrySet()) { |
| 37 | + int frequency = e.getValue(); |
| 38 | + if (bucket[frequency] == null) { |
| 39 | + bucket[frequency] = new ArrayList<Integer>(); |
| 40 | + } |
| 41 | + bucket[frequency].add(e.getKey()); |
| 42 | + } |
| 43 | + List<Integer> result = new ArrayList<>(); |
| 44 | + for (int i = bucket.length - 1; i >= 0 && result.size() < k; i--) { |
| 45 | + if (bucket[i] != null) { |
| 46 | + for (int j = 0; j < bucket[i].size(); j++) { |
| 47 | + result.add((int) bucket[i].get(j)); |
| 48 | + } |
| 49 | + } |
50 | 50 | }
|
51 |
| - } |
52 | 51 |
|
53 |
| - return result; |
| 52 | + return result; |
| 53 | + } |
54 | 54 | }
|
55 | 55 |
|
56 |
| - // Approach 2: use hashtable and heap |
57 |
| - |
58 |
| - /** |
59 |
| - * Bonus tips on how to write a priority queue: |
60 |
| - * <p> |
61 |
| - * Tip1: |
62 |
| - * it should be like this: |
63 |
| - * PriorityQueue's angle brackets should be left blank, the type should be in |
64 |
| - * Comparator's angle brackets and the compare method should be in Comparator's |
65 |
| - * brackets. new PriorityQueue<>(new Comparator<int[]>(){ public int |
66 |
| - * compare(int[] o1, int[] o2){ } }) |
67 |
| - * <p> |
68 |
| - * Tip2: |
69 |
| - * if you want things in DEscending order, then if(01 > o2), it should return -1 |
70 |
| - * if Ascending order, then if(01 > o2), it should return 1 |
71 |
| - */ |
72 |
| - public List<Integer> topKFrequent_using_heap(int[] nums, int k) { |
73 |
| - Map<Integer, Integer> map = new HashMap(); |
74 |
| - Queue<Entry<Integer, Integer>> heap = new PriorityQueue<>(new Comparator<Entry<Integer, Integer>>() { |
75 |
| - @Override |
76 |
| - public int compare(Entry<Integer, Integer> o1, Entry<Integer, Integer> o2) { |
77 |
| - if (o1.getValue() > o2.getValue()) { |
78 |
| - return -1; |
79 |
| - } else if (o1.getValue() < o2.getValue()) { |
80 |
| - return 1; |
81 |
| - } |
82 |
| - return 0; |
| 56 | + public static class Solution2 { |
| 57 | + /** |
| 58 | + * Use hashtable and heap |
| 59 | + */ |
| 60 | + public List<Integer> topKFrequent(int[] nums, int k) { |
| 61 | + // construct the frequency map first, and then iterate through the map |
| 62 | + // and put them into the heap, this is O(n) |
| 63 | + Map<Integer, Integer> map = new HashMap(); |
| 64 | + for (int num : nums) { |
| 65 | + map.put(num, map.getOrDefault(num, 0) + 1); |
83 | 66 | }
|
84 |
| - }); |
85 | 67 |
|
86 |
| - // construct the frequency map first, and then iterate through the map |
87 |
| - // and put them into the heap, this is O(n) |
88 |
| - for (int x : nums) { |
89 |
| - if (map.containsKey(x)) { |
90 |
| - map.put(x, map.get(x) + 1); |
91 |
| - } else { |
92 |
| - map.put(x, 1); |
| 68 | + // build heap, this is O(logn) |
| 69 | + Queue<Entry<Integer, Integer>> heap = new PriorityQueue<>((o1, o2) -> { |
| 70 | + if (o1.getValue() > o2.getValue()) { |
| 71 | + return -1; |
| 72 | + } else if (o1.getValue() < o2.getValue()) { |
| 73 | + return 1; |
| 74 | + } else { |
| 75 | + return 0; |
| 76 | + } |
| 77 | + }); |
| 78 | + for (Entry<Integer, Integer> entry : map.entrySet()) { |
| 79 | + heap.offer(entry); |
93 | 80 | }
|
94 |
| - } |
95 |
| - |
96 |
| - // build heap, this is O(n) as well |
97 |
| - for (Entry<Integer, Integer> entry : map.entrySet()) { |
98 |
| - heap.offer(entry); |
99 |
| - } |
100 | 81 |
|
101 |
| - List<Integer> res = new ArrayList(); |
102 |
| - while (k-- > 0) { |
103 |
| - res.add(heap.poll().getKey()); |
| 82 | + List<Integer> res = new ArrayList(); |
| 83 | + while (k-- > 0) { |
| 84 | + res.add(heap.poll().getKey()); |
| 85 | + } |
| 86 | + return res; |
104 | 87 | }
|
105 |
| - return res; |
106 |
| - } |
107 |
| - |
108 |
| - public static void main(String[] args) { |
109 |
| - int[] nums = new int[]{3, 0, 1, 0}; |
110 |
| - _347 test = new _347(); |
111 |
| - test.topKFrequent_using_heap(nums, 1); |
112 |
| -// test.topKFrequent_using_bucket(nums, 1); |
113 | 88 | }
|
114 | 89 |
|
115 | 90 | }
|
0 commit comments