|
9 | 9 | import java.util.PriorityQueue;
|
10 | 10 | import java.util.Queue;
|
11 | 11 |
|
12 |
| -/**Given a non-empty array of integers, return the k most frequent elements. |
| 12 | +/** |
| 13 | + * 347. Top K Frequent Elements |
| 14 | + * |
| 15 | + * Given a non-empty array of integers, return the k most frequent elements. |
13 | 16 |
|
14 | 17 | For example,
|
15 | 18 | Given [1,1,1,2,2,3] and k = 2, return [1,2].
|
16 | 19 |
|
17 | 20 | Note:
|
18 | 21 | You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
|
19 | 22 | Your algorithm's time complexity must be better than O(n log n), where n is the array's size.*/
|
20 |
| -public class TopKFrequentElements { |
| 23 | + |
| 24 | +public class _347 { |
21 | 25 | // Approach 1: use buckets to hold numbers of the same frequency
|
22 | 26 | /**Attn: we must use a simple array to solve this problem, instead of using List<List<Integer>>,
|
23 | 27 | * we have to use List<Integer>[], otherwise, cases like this one: [-1,-1]
|
@@ -98,7 +102,7 @@ else if (o1.getValue() < o2.getValue())
|
98 | 102 |
|
99 | 103 | public static void main(String[] args) {
|
100 | 104 | int[] nums = new int[] { 3, 0, 1, 0 };
|
101 |
| - TopKFrequentElements test = new TopKFrequentElements(); |
| 105 | + _347 test = new _347(); |
102 | 106 | test.topKFrequent_using_heap(nums, 1);
|
103 | 107 | // test.topKFrequent_using_bucket(nums, 1);
|
104 | 108 | }
|
|
0 commit comments