Skip to content

Commit

Permalink
347. Top K Frequent Elements update.
Browse files Browse the repository at this point in the history
  • Loading branch information
liusishan committed Feb 26, 2019
1 parent 5d01961 commit 19e57ce
Showing 1 changed file with 8 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import java.util.LinkedList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.TreeMap;
import java.util.*;

/**
* @Auther: lss
Expand All @@ -10,22 +7,20 @@
*/
public class Solution {

private class Freq implements Comparable<Freq> {
private class Freq {
int e, freq;

public Freq(int e, int freq) {
this.e = e;
this.freq = freq;
}
}

private class FreqComparator implements Comparator<Freq> {

@Override
public int compareTo(Freq o) {
if (this.freq < o.freq)
return -1;
else if (this.freq > o.freq)
return 1;
else
return 0;
public int compare(Freq o1, Freq o2) {
return o1.freq - o2.freq;
}
}

Expand All @@ -39,7 +34,7 @@ public List<Integer> topKFrequent(int[] nums, int k) {
map.put(num, 1);
}

PriorityQueue<Freq> pq = new PriorityQueue<>();
PriorityQueue<Freq> pq = new PriorityQueue<>(new FreqComparator());
for (int key : map.keySet()) {
if (pq.size() < k)
pq.add(new Freq(key, map.get(key)));
Expand Down

0 comments on commit 19e57ce

Please sign in to comment.