Skip to content

Commit 127fc37

Browse files
refactor 347
1 parent 7f23e25 commit 127fc37

File tree

1 file changed

+3
-10
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+3
-10
lines changed

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

+3-10
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class _347 {
2525
public static class Solution1 {
2626
/**
2727
* Use buckets to hold numbers of the same frequency
28+
* It's averaged at 30 ms on Leetcode.
2829
*/
2930
public List<Integer> topKFrequent(int[] nums, int k) {
3031
Map<Integer, Integer> map = new HashMap();
@@ -55,7 +56,7 @@ public List<Integer> topKFrequent(int[] nums, int k) {
5556

5657
public static class Solution2 {
5758
/**
58-
* Use hashtable and heap
59+
* Use hashtable and heap, it's averaged at 100 ms on Leetocde.
5960
*/
6061
public List<Integer> topKFrequent(int[] nums, int k) {
6162
// construct the frequency map first, and then iterate through the map
@@ -66,15 +67,7 @@ public List<Integer> topKFrequent(int[] nums, int k) {
6667
}
6768

6869
// 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-
});
70+
Queue<Entry<Integer, Integer>> heap = new PriorityQueue<>((o1, o2) -> o2.getValue() - o1.getValue());
7871
for (Entry<Integer, Integer> entry : map.entrySet()) {
7972
heap.offer(entry);
8073
}

0 commit comments

Comments
 (0)