Skip to content

Commit bb01f36

Browse files
refactor 347
1 parent c5ecfe5 commit bb01f36

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

Lines changed: 3 additions & 10 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy