leetcode 692. Top K Frequent Words

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.

//首先想到用set红黑树排序来做
//这个方法的复杂度是nlogn
class word{
public:
    string content;
    int frequent;
    
    word(string a, int b):content(a), frequent(b) {}
    bool operator<(const word b) const
    {
        if (this->frequent == b.frequent) {
            int k = 0;
            while (k < this->content.size() && k < b.content.size()){
                if (this->content[k] == b.content[k])
                {
                    k++;
                    continue;
                }
                return this->content[k] < b.content[k];
            }
        }
        return this->frequent > b.frequent;
    }
};

class Solution {
public:
    vector<string> topKFrequent(vector<string>& wordss, int k)
    {   //第一阶段
        unordered_map<string, int> mp; //hash O(n)
        for (auto word : wordss)
        {
            mp[word]++;
        }
        //第二阶段
        multiset<word> st;  //必须是multiset,不然会出错。某些情况下,相同的frequence是不能添加进set的
        for (auto it : mp)  //O(nlogn)
        {
            st.insert(word(it.first, it.second));
        }
        for (auto it : st)
            cout<<it.content<<"-"<<it.frequent<<"   ";
        //第三阶段
        vector<string> ret;
        for (auto it : st)
        {
            ret.push_back( it.content );
            if (--k == 0) break;
        }
        return ret;
    }
};


//一看到复杂度nlogk,就应该知道用堆排序,维护一个k大小的堆。
//应该想到堆排序。
//make_heap:O(k)
//pop_heap:O(logk)
//push_heap:O(logk)
class word{
public:
    string content;
    int frequent;

    word(string a, int b):content(a), frequent(b) {}
};

bool compare(const word a, const word b)
{
    if (a.frequent == b.frequent) {
        int k = 0;
        while (k < a.content.size() && k < b.content.size()){
            if (a.content[k] == b.content[k])
            {
                k++;
                continue;
            }
            return a.content[k] < b.content[k];
        }
        return k == a.content.size();
    }
    return a.frequent > b.frequent;
}

class Solution {
public:
    vector<string> topKFrequent(vector<string>& wordss, int k)
    {   //第一阶段
        unordered_map<string, int> mp; //hash O(n)
        for (auto word : wordss)
        {
            mp[word]++;
        }
        //第二阶段
        vector<word> vt;
        for (auto it : mp)
        {
            if (vt.size() < k)
            {
                vt.push_back(word(it.first, it.second));
                if (vt.size() == k)
                    make_heap(vt.begin(), vt.end(), compare);
            }
            else
            {
                if ( compare(word(it.first, it.second), vt[0]) ) //当前遍历的值比heap里面最小的大,就应该替换出这个最小的
                {
                    pop_heap(vt.begin(), vt.end(), compare);
                    vt.pop_back();
                    vt.push_back(word(it.first, it.second));
                    push_heap(vt.begin(), vt.end(), compare);   //重新构成堆
                }
            }
        }
        //第三阶段 弹出堆顶,从后往前放置到ret中
        vector<string> ret(k, "");
        for (int i = k - 1; i >= 0; i --)
        {
            pop_heap(vt.begin(), vt.end(), compare);
            ret[i] = vt.back().content;
            vt.pop_back();
        }
        return ret;
    }
};



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值