public List<Integer> topKFrequent(int[] nums, int k){ // number -> frequency Map<Integer,Integer> frequencies = new HashMap<>(); for (int num : nums) { int f = frequencies.getOrDefault(num, 0) + 1; frequencies.put(num, f); } // min heap PriorityQueue<Integer> heap = new PriorityQueue(new Comparator<Integer>() { publicintcompare(Integer a, Integer b){ // compare a,b by frequency return frequencies.get(a) - frequencies.get(b); } }); // put each number to heap for (int num : frequencies.keySet()) { heap.add(num); if (heap.size() > k) { heap.poll(); } } // heap to list returnnew ArrayList<Integer>(heap); }
classSolution{ public List<Integer> topKFrequent(int[] nums, int k){ // number -> frequency Map<Integer,Integer> frequencies = new HashMap<>(); for (int num : nums) { int f = frequencies.getOrDefault(num, 0) + 1; frequencies.put(num, f); } // [num, freq], [num, freq], ... int[][] array = newint[frequencies.size()][2]; int index = 0; for (Map.Entry<Integer, Integer> entry : frequencies.entrySet()) { array[index++] = newint[]{entry.getKey(), entry.getValue()}; } // quick select quickSelect(array, 0, array.length-1, k); // for (int[] a : array) { // System.out.print(a[0] + ":" + a[1] + ", "); // } List<Integer> result = new ArrayList<Integer>(k); for (int i = 0; i < k; ++i) { result.add(array[i][0]); } return result; }
publicvoidquickSelect(int[][] array, int start, int end, int k){ while (start < end) { int pivot = partition(array, start, end); if (pivot == k) { break; } elseif (pivot < k) { start = pivot + 1; } else { end = pivot - 1; } } }
publicintpartition(int[][] array, int start, int end){ swap(array, start, new Random().nextInt(end - start) + start); int pivot = array[start][1]; int i = start, j = end; while (i < j) { while (i < j && array[j][1] <= pivot) --j; while (i < j && array[i][1] >= pivot) ++i; if (i < j) { swap(array, i, j); } } swap(array, start, i); return i; }
publicvoidswap(int[][] array, int a, int b){ int[] tmp = array[a]; array[a] = array[b]; array[b] = tmp; } }
classSolution{ public List<Integer> topKFrequent(int[] nums, int k){ // number -> frequency Map<Integer,Integer> frequencies = new HashMap<>(); for (int num : nums) { int f = frequencies.getOrDefault(num, 0) + 1; frequencies.put(num, f); } // frequency = 0 ~ N // create N+1 buckets // buckets[i] : numbers with freq = i List<Integer>[] buckets = new List[nums.length + 1]; for (Map.Entry<Integer, Integer> entry : frequencies.entrySet()) { int f = entry.getValue(); int num = entry.getKey(); if (buckets[f] == null) { buckets[f] = new LinkedList<>(); } buckets[f].add(num); } // read k most frequent elements from buckets List<Integer> result = new ArrayList<Integer>(k); for (int i = buckets.length-1; i >= 0; --i) { List<Integer> bucket = buckets[i]; if (bucket == null) { continue; } int remain = k - result.size(); if (remain == bucket.size()) { result.addAll(bucket); break; } elseif (remain > bucket.size()) { result.addAll(bucket); } else { result.addAll(bucket.subList(0, remain-1)); break; } } return result; } }