zoukankan      html  css  js  c++  java
  • [LC] 692. Top K Frequent Words

    Given a non-empty list of words, return the k most frequent elements.

    Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, then the word with the lower alphabetical order comes first.

    Example 1:

    Input: ["i", "love", "leetcode", "i", "love", "coding"], k = 2
    Output: ["i", "love"]
    Explanation: "i" and "love" are the two most frequent words.
        Note that "i" comes before "love" due to a lower alphabetical order.
    

    Example 2:

    Input: ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], k = 4
    Output: ["the", "is", "sunny", "day"]
    Explanation: "the", "is", "sunny" and "day" are the four most frequent words,
        with the number of occurrence being 4, 3, 2 and 1 respectively.
    

    Note:

    1. You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
    2. Input words contain only lowercase letters.

    Follow up:

    1. Try to solve it in O(n log k) time and O(n) extra space.
     1 import collections
     2 import heapq
     3 
     4 class Element(object):
     5     def __init__(self, word, freq):
     6         self.word = word
     7         self.freq = freq
     8     
     9     def __lt__(self, other):
    10         if self.freq != other.freq:
    11             return self.freq < other.freq
    12         return other.word < self.word
    13 
    14 class Solution(object):
    15     def topKFrequent(self, words, k):
    16         """
    17         :type words: List[str]
    18         :type k: int
    19         :rtype: List[str]
    20         """
    21         my_dict = {}
    22         for word in words:
    23             if word in my_dict:
    24                 my_dict[word] += 1
    25             else:
    26                 my_dict[word] = 1 
    27         
    28         freqs = []
    29         for word, count in my_dict.items():
    30             heapq.heappush(freqs, (Element(word, count)))
    31             if len(freqs) > k:
    32                 heapq.heappop(freqs)
    33         res = []
    34         for _ in range(k):
    35             res.append(heapq.heappop(freqs).word)
    36         res.reverse()
    37         return res
    38     
    39         

    Solution 2:

    O(klogN)

    class Solution {
        public List<String> topKFrequent(String[] words, int k) {
        Map<String, Integer> map = new HashMap<>();
        for (String s: words) {
          map.put(s, map.getOrDefault(s, 0) + 1);
        }
        // keep a top frequency heap
        PriorityQueue<Map.Entry<String, Integer>> pq = new PriorityQueue<>((a, b) -> 
          a.getValue() == b.getValue() ? a.getKey().compareTo(b.getKey()): b.getValue() - a.getValue()
        );
        pq.addAll(map.entrySet());
        List<String> res = new ArrayList<>();
        int i = 0;
        while (i < k) {
            res.add(pq.poll().getKey());
            i += 1;
        }
        return res;        
        }
    }
  • 相关阅读:
    JAVA 字符处理
    android:visibility 隐藏 可见 不可见
    Android中 int 和 String 互相转换的多种方法
    日期月和日补零
    【程序】程序报错 error:-1610149839 等大负数
    【IE】将IE11改为64位
    【linux】CentOS网络配置
    【linux】CentOS虚拟机eth0 提示Device does not seem to be present
    【SUSE】关闭防火墙
    【走马观花】十月八日通州雾
  • 原文地址:https://www.cnblogs.com/xuanlu/p/11577480.html
Copyright © 2011-2022 走看看