Skip to content

Commit 144e179

Browse files
refactor 347
1 parent 279ed91 commit 144e179

File tree

1 file changed

+52
-64
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+52
-64
lines changed

src/main/java/com/fishercoder/solutions/_347.java

Lines changed: 52 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -8,75 +8,63 @@
88
import java.util.PriorityQueue;
99
import java.util.Queue;
1010

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-
2311
public class _347 {
2412

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+
}
3523

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+
}
5240

53-
return result;
54-
}
55-
}
41+
return result;
42+
}
43+
}
5644

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+
}
6856

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+
}
7462

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+
}
8270
}

0 commit comments

Comments
 (0)