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;        
        }
    }
  • 相关阅读:
    122.CSS书写顺序及其好处
    120.节流和防抖,Vue中如何添加节流和防抖
    119.css如何使图片固定为正方形
    112.前端css优先级
    vscode设置格式化单引号
    113.CSS怪象记录
    116.vue : 无法加载文件 C:UsersAdministratorAppDataRoaming pmvue.ps1,因为在此系统上禁止运行脚本。
    距离不是一个连续的物理量(Distance is not a continuous physical quantity)
    南七学校信息
    RedisTemplate 序列化问题
  • 原文地址:https://www.cnblogs.com/xuanlu/p/11577480.html
Copyright © 2011-2022 走看看