@@ -0,0 +1,1100 @@
+package com.flang.sort;%0A%0Aimport java.util.*;%0A%0Apublic class TopKFrequentWords %7B%0A%0A    public List%3CString%3E topKFrequent(String%5B%5D words, int k) %7B%0A        Map%3CString, Integer%3E map = new HashMap%3C%3E();%0A        for (String word : words) %7B%0A            map.put(word, map.getOrDefault(word, 0) + 1);%0A        %7D%0A%0A        PriorityQueue%3CString%3E queue = new PriorityQueue%3C%3E(new Comparator%3CString%3E() %7B%0A            @Override%0A            public int compare(String o1, String o2) %7B%0A                int count1 = map.get(o1);%0A                int count2 = map.get(o2);%0A                if (count1 == count2) %7B%0A                    return o2.compareTo(o1);%0A                %7D%0A                return count1 - count2;%0A            %7D%0A        %7D);%0A%0A        for (Map.Entry%3CString, Integer%3E entry : map.entrySet()) %7B%0A            queue.offer(entry.getKey());%0A            if (queue.size() %3E k) %7B%0A                queue.poll();%0A            %7D%0A        %7D%0A%0A        List%3CString%3E rst = new ArrayList%3C%3E();%0A        while(!queue.isEmpty()) %7B%0A            rst.add(queue.poll());%0A        %7D%0A        Collections.reverse(rst);%0A        return rst;%0A    %7D%0A%7D%0A

Create TopKFrequentWords.java