zoukankan      html  css  js  c++  java
  • 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.

    Approach #1: C++.[unordered_map]

    class Solution {
    public:
        vector<string> topKFrequent(vector<string>& words, int k) {
            if (words.empty()) return {};
            unordered_map<string, int> m;
            for (string word : words) {
                m[word]++;
            }
            vector<pair<string, int>> temp(m.begin(), m.end());
            sort(temp.begin(), temp.end(), cmp);
            vector<string> ans;
            for (int i = 0; i < k; ++i) {
                ans.push_back(temp[i].first);
            }
            return ans;
        }
        
    private:
        static bool cmp(pair<string, int> a, pair<string, int> b) {
            if (a.second == b.second) return a.first < b.first;
            return a.second > b.second;
        }
    };
    

      

    Approach #2: Java. [heap]

    class Solution {
        public List<String> topKFrequent(String[] words, int k) {
            Map<String, Integer> count = new HashMap();
            for (String word : words) {
                count.put(word, count.getOrDefault(word, 0) + 1);
            }
            PriorityQueue<String> heap = new PriorityQueue<String>(
                (w1, w2)->count.get(w1).equals(count.get(w2)) ?
                w2.compareTo(w1) : count.get(w1) - count.get(w2) );
            
            for (String word : count.keySet()) {
                heap.offer(word);
                if (heap.size() > k) heap.poll();
            }
            
            List<String> ans = new ArrayList();
            while (!heap.isEmpty()) ans.add(heap.poll());
            Collections.reverse(ans);
            return ans;
        }
    }
    

      

    Approach #3: Python.

    import collections
    import heapq
    class Solution:
        # Time Complexity = O(n + nlogk)
        # Space Complexity = O(n)
        def topKFrequent(self, words, k):
            count = collections.Counter(words)
            heap = []
            for key, value in count.items():
                heapq.heappush(heap, Word(value, key))
                if len(heap) > k:
                    heapq.heappop(heap)
            res = []
            for _ in range(k):
                res.append(heapq.heappop(heap).word)
            return res[::-1]
    
    class Word:
        def __init__(self, freq, word):
            self.freq = freq
            self.word = word
    
        def __lt__(self, other):
            if self.freq == other.freq:
                return self.word > other.word
            return self.freq < other.freq
    
        def __eq__(self, other):
            return self.freq == other.freq and self.word == other.word
    

      

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    angularJS处理table中checkbox的选中状态
    大公司常见表格样式
    github:当你想要使用VSCODE开心提交代码时,出现Git:git@github.com:Permission denied(publickey)解决方案
    angularjs 使用angular-sortable-view实现拖拽效果(包括拖动完成后的方法使用)
    sortable结合angularjs实现拖动排序
    promise的一个简单易懂实例
    angular-ui-select (系列二)远程搜索,页面方框显示的值跟传给后台的值不一样解决方案
    leetcode刷题笔记二十九 两数相除
    scala学习 包和引入
    leetcode刷题笔记二十六 、 二十七、 二十八
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10158396.html
Copyright © 2011-2022 走看看