Top K Frequent Words

本文介绍了一种通过哈希表和优先队列实现的算法,用于筛选给定列表中频率最高的k个单词,并按频率和字母顺序进行排序。

Given a list of words and an integer k, return the top k frequent words in the list.

You should order the words by the frequency of them in the return list, the most frequent one comes first. If two words has the same frequency, the one with lower alphabetical order come first.

[
    "yes", "lint", "code",
    "yes", "code", "baby",
    "you", "baby", "chrome",
    "safari", "lint", "code",
    "body", "lint", "code"
]

for k = 3, return ["code", "lint", "baby"].

for k = 4, return ["code", "lint", "baby", "yes"],

思路:Hash map + PriorityQueue(最小元素在顶部) + 定义 compare

 1 class Pair {
 2     String key;
 3     int value;
 4     
 5     Pair(String key, int value) {
 6         this.key = key;
 7         this.value = value;
 8     }
 9 }
10 
11 public class Solution {
12     /**
13      * @param words an array of string
14      * @param k an integer
15      * @return an array of string
16      */
17      
18     private Comparator<Pair> pairComparator = new Comparator<Pair>() {
19         public int compare(Pair left, Pair right) {
20             if (left.value != right.value) {
21                 return left.value - right.value > 0 ? 1 : -1;
22             }
23             return right.key.compareTo(left.key);
24         }
25     };
26     
27     public String[] topKFrequentWords(String[] words, int k) {
28         if (k == 0) {
29             return new String[0];
30         }
31         
32         HashMap<String, Integer> counter = new HashMap<>();
33         for (String word : words) {
34             if (counter.containsKey(word)) {
35                 counter.put(word, counter.get(word) + 1);
36             } else {
37                 counter.put(word, 1);
38             }
39         }
40         
41         PriorityQueue<Pair> Q = new PriorityQueue<Pair>(k, pairComparator);
42         for (String word : counter.keySet()) {
43             Pair peak = Q.peek();
44             Pair newPair = new Pair(word, counter.get(word));
45             if (Q.size() < k) {
46                 Q.add(newPair);
47             } else if (pairComparator.compare(newPair, peak) > 0) {
48                 Q.poll();
49                 Q.add(new Pair(word, counter.get(word)));
50             }
51         }
52         
53         String[] result = new String[k];
54         int index = 0;
55         while (!Q.isEmpty()) {
56             result[index++] = Q.poll().key;
57         }
58         for (int i = 0; i < result.length / 2; i++) {
59             String tmp = result[i];
60             result[i] = result[result.length - i - 1];
61             result[result.length - i - 1] = tmp;
62         }
63         return result;
64     }
65 }

 

转载于:https://www.cnblogs.com/FLAGyuri/p/5358465.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值