|
| 1 | +packagemedium; |
| 2 | + |
| 3 | +importjava.util.ArrayList; |
| 4 | +importjava.util.Comparator; |
| 5 | +importjava.util.HashMap; |
| 6 | +importjava.util.List; |
| 7 | +importjava.util.Map; |
| 8 | +importjava.util.Map.Entry; |
| 9 | +importjava.util.PriorityQueue; |
| 10 | +importjava.util.Queue; |
| 11 | + |
| 12 | +publicclassTopKFrequentElements { |
| 13 | +// Approach 1: use buckets to hold numbers of the same frequency |
| 14 | +/**Attn: we must use a simple array to solve this problem, instead of using List<List<Integer>>, |
| 15 | + * we have to use List<Integer>[], otherwise, cases like this one: [-1,-1] |
| 16 | + * 1 will fail due to the fact that ArrayList.get(i), |
| 17 | + * this i must be a non-negative number, however, in simple arrays, the index could be negative. |
| 18 | + * Although in this question, frequency will be at least 1, but still in problems like this where bucket sort |
| 19 | + * works the best, you should use List<Integer>[], this will simplify the code.*/ |
| 20 | +publicList<Integer>topKFrequent_using_bucket(int[]nums,intk) { |
| 21 | +Map<Integer,Integer>map =newHashMap(); |
| 22 | +for(inti :nums){ |
| 23 | +map.put(i,map.getOrDefault(i,0)+1); |
| 24 | +} |
| 25 | + |
| 26 | +ArrayList[]bucket =newArrayList[nums.length+1]; |
| 27 | +for(Map.Entry<Integer,Integer>e :map.entrySet()){ |
| 28 | +intfrequency =e.getValue(); |
| 29 | +if(bucket[frequency] ==null){ |
| 30 | +bucket[frequency] =newArrayList<Integer>(); |
| 31 | +} |
| 32 | +bucket[frequency].add(e.getKey()); |
| 33 | +} |
| 34 | +List<Integer>result =newArrayList<Integer>(); |
| 35 | +for(inti =bucket.length-1;i >=0 &&result.size() <k;i--){ |
| 36 | +if(bucket[i] !=null)result.addAll(bucket[i]); |
| 37 | +} |
| 38 | + |
| 39 | +returnresult; |
| 40 | +} |
| 41 | + |
| 42 | +// Approach 2: use hashtable and heap |
| 43 | +/** |
| 44 | + * Bonus tips on how to write a priority queue: |
| 45 | + * |
| 46 | + * Tip1: |
| 47 | + * it should be like this: |
| 48 | + * PriorityQueue's angle brackets should be left blank, the type should be in |
| 49 | + * Comparator's angle brackets and the compare method should be in Comparator's |
| 50 | + * brackets. new PriorityQueue<>(new Comparator<int[]>(){ public int |
| 51 | + * compare(int[] o1, int[] o2){ } }) |
| 52 | + * |
| 53 | + * Tip2: |
| 54 | + * if you want things in DEscending order, then if(01 > o2), it should return -1 |
| 55 | + * if Ascending order, then if(01 > o2), it should return 1 |
| 56 | + */ |
| 57 | +publicList<Integer>topKFrequent_using_heap(int[]nums,intk) { |
| 58 | +Map<Integer,Integer>map =newHashMap(); |
| 59 | +Queue<Map.Entry<Integer,Integer>>heap =newPriorityQueue<>(newComparator<Map.Entry<Integer,Integer>>() { |
| 60 | +@Override |
| 61 | +publicintcompare(Entry<Integer,Integer>o1,Entry<Integer,Integer>o2) { |
| 62 | +if (o1.getValue() >o2.getValue()) |
| 63 | +return -1; |
| 64 | +elseif (o1.getValue() <o2.getValue()) |
| 65 | +return1; |
| 66 | +return0; |
| 67 | +} |
| 68 | +}); |
| 69 | + |
| 70 | +// construct the frequency map first, and then iterate through the map |
| 71 | +// and put them into the heap, this is O(n) |
| 72 | +for (intx :nums) { |
| 73 | +if (map.containsKey(x)) |
| 74 | +map.put(x,map.get(x) +1); |
| 75 | +else |
| 76 | +map.put(x,1); |
| 77 | +} |
| 78 | + |
| 79 | +// build heap, this is O(n) as well |
| 80 | +for (Map.Entry<Integer,Integer>entry :map.entrySet()) { |
| 81 | +heap.offer(entry); |
| 82 | +} |
| 83 | + |
| 84 | +List<Integer>res =newArrayList<Integer>(); |
| 85 | +while (k-- >0) { |
| 86 | +res.add(heap.poll().getKey()); |
| 87 | +} |
| 88 | +returnres; |
| 89 | +} |
| 90 | + |
| 91 | +publicstaticvoidmain(String[]args) { |
| 92 | +int[]nums =newint[] {3,0,1,0 }; |
| 93 | +TopKFrequentElementstest =newTopKFrequentElements(); |
| 94 | +test.topKFrequent_using_heap(nums,1); |
| 95 | +//test.topKFrequent_using_bucket(nums, 1); |
| 96 | +} |
| 97 | + |
| 98 | +} |