import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
public class KMostFrequentStringsMaxHeap {
public class Pair implements Comparable<Pair> {
String str;
int freq;
public Pair(String str, int freq) {
this.str = str;
this.freq = freq;
}
@Override
public int compareTo(Pair other) {
if (this.freq == other.freq) {
return other.str.compareTo(this.str);
}
return Integer.compare(this.freq, other.freq);
}
}
public List<String> kMostFrequentStringsMaxHeap(String[] strs, int k) {
List<String> res = new ArrayList<>();
Map<String, Integer> freqs = new HashMap<>();
for (String str : strs) {
freqs.put(str, freqs.getOrDefault(str, 0) + 1);
}
PriorityQueue<Pair> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
for (Map.Entry<String, Integer> entry : freqs.entrySet()) {
maxHeap.add(new Pair(entry.getKey(), entry.getValue()));
}
for (int i = 0; i < k; i++) {
res.add(maxHeap.poll().str);
}
return res;
}
}