692. 前K个高频单词

给一非空的单词列表,返回前 个出现次数最多的单词。

返回的答案应该按单词出现频率由高到低排序。如果不同的单词有相同出现频率,按字母顺序排序。

示例 1:

 

示例 2:

 

注意:

  1. 假定 k 总为有效值, 1 ≤ k ≤ 集合元素数。
  2. 输入的单词均由小写字母组成。

 

扩展练习:尝试以 O(n log k) 时间复杂度和 O(n) 空间复杂度解决.

(1)There is a pretty simple Brute Force solution - create a map (word->occurance), then put EntrySet to List and sort this list with a custom comparator. The time complexity is O(N log N).

(2)But since we need to return k popular items only - we don't need to sort all items. So, the second idea is to use Heap with limited capacity k. The time complexity is O(N log K)

(3)The third solution is to use Bucket Sort - O(N + K log K)

(1)

   Map<String, Integer> count = new HashMap();
        for (String word: words) {
            count.put(word, count.getOrDefault(word, 0) + 1);
        }
        List<String> candidates = new ArrayList(count.keySet());
        Collections.sort(candidates, (w1, w2) -> count.get(w1).equals(count.get(w2)) ?
                w1.compareTo(w2) : count.get(w2) - count.get(w1));


        return candidates.subList(0, k);

(2)
Map<String, Integer> map = new HashMap<>();
        for(String w : words) map.put(w, map.getOrDefault(w, 0)+1);
    
        PriorityQueue<Map.Entry<String, Integer>> pq = new PriorityQueue<>((e1, e2)->{
            int res = e1.getValue() - e2.getValue();
            if(res==0) return e2.getKey().compareTo(e1.getKey());
            return res;
        });
        
        for(Map.Entry e : map.entrySet()) {
            pq.add(e);
            if (pq.size() > k) pq.poll();
        }
        
        List<String> res = new ArrayList<>();
        for(int i=0;i<k;i++) res.add(pq.poll().getKey());
        Collections.reverse(res);
        return res; 

(3)public List<String> topKFrequent(String[] words, int k) {

        Map<String,Integer> map = new HashMap<>();
        for(String word:words)
            map.put(word,map.getOrDefault(word,0)+1);
        List<String>[] buckets = new List[words.length+1];
        for(String key:map.keySet()){
            int freq = map.get(key);
            if(buckets[freq]==null)
                buckets[freq] = new LinkedList<>();
            buckets[freq].add(key);
        }
        List<String> ans = new LinkedList<>();
        int c = 0;
        for(int i = buckets.length-1;i>=0&&k>0;i--){
            if(buckets[i]!=null){
                Collections.sort(buckets[i]);
                for(int j=0;j<buckets[i].size();j++) {
                    if(c==k) 
                        return ans;
                    ans.add(buckets[i].get(j));
                    c++;
                }
            }
        }
        return ans ;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值