首页 > 其他分享 >LeetCode 347. 前 K 个高频元素

LeetCode 347. 前 K 个高频元素

时间:2023-07-21 12:22:18浏览次数:30  
标签:vector nums int res 347 mp auto 高频 LeetCode

快排思想

  • 注意,这里是倒序排序,因此应该while(nums[i].cnt>x);
class Solution {
public:
    struct element
    {
        int val,cnt;
        element(int a,int b)
        {
            val=a;
            cnt=b;
        }
    };
    vector<int> res;
    void quick(vector<element>& nums, int l,int r,int k)
    {
        if(l==r)
        {
            res.push_back(nums[l].val);
            return;
        }
        int x=nums[(l+r+1)>>1].cnt;
        int i=l-1,j=r+1;
        while(i<j)
        {
            do i++;while(nums[i].cnt>x);
            do j--;while(nums[j].cnt<x);
            if(i<j) swap(nums[i],nums[j]);
        }
        cout<<i<<' '<<j<<' '<<k<<endl;
        if(i-l==k)
        {
            for(int k=l;k<i;k++)
                res.push_back(nums[k].val);
            return;
        }
        else if(i-l>k)
            quick(nums,l,i-1,k);
        else
        {
            for(int k=l;k<i;k++)
                res.push_back(nums[k].val);
            quick(nums,i,r,k-i+l);
        }
    }
    vector<int> topKFrequent(vector<int>& nums, int k) {
        unordered_map<int,int> mp;
        for(auto x:nums)    mp[x]++;
        vector<element> q;
        for(auto x:mp)  q.push_back({x.first,x.second});
        quick(q,0,q.size()-1,k);
        return res;
    }
};

class Solution {
public:
    vector<int> topKFrequent(vector<int>& nums, int k) {
        unordered_map<int,int> mp;
        for(auto x:nums)    mp[x]++;
        priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;
        for(auto item:mp)//维护大小为k的小根堆
        {
            if(q.size()<k)  q.push(make_pair(item.second,item.first));
            else if(item.second>q.top().first)
            {
                q.pop();
                q.push(make_pair(item.second,item.first));
            }
        }
        vector<int> res;
        for(int i=0;i<k;i++)
        {
            res.push_back(q.top().second);
            q.pop();
        }
        return res;
    }
}; 

我的解法

class Solution {
public:
    vector<int> topKFrequent(vector<int>& nums, int k) {
        unordered_map<int,int> hashmap;
        multimap<int,int> mp;
        vector<int> res;
        for(auto x:nums)    hashmap[x]++;
        for(auto item:hashmap)
            mp.insert(make_pair(item.second,item.first));
            
        for(auto i=mp.rbegin();i!=mp.rend()&&k;k--,i++)
            res.push_back(i->second);            
        return res;
    }
};

标签:vector,nums,int,res,347,mp,auto,高频,LeetCode
From: https://www.cnblogs.com/tangxibomb/p/17570975.html

相关文章

  • LeetCode -- 773. 滑动谜题
     启发式搜索 classSolution{structNode{stringstr;intx,y;intval;};intn=2,m=3;stringe="123450";intdx[4]={-1,0,1,0};intdy[4]={0,1,0,-1};intf(stringstr){intres=0;for(inti=0;i<......
  • LeetCode 第66题. 加1
    题目:给定一个由 整数 组成的 非空 数组所表示的非负整数,在该数的基础上加一。最高位数字存放在数组的首位,数组中每个元素只存储单个数字。你可以假设除了整数0之外,这个整数不会以零开头。示例 1:输入:digits=[1,2,3]输出:[1,2,4]示例 2:输入:digits=[4,3,2,1]输出:[4,3,2,......
  • leetcode-1518-easy
    WaterBottlesTherearenumBottleswaterbottlesthatareinitiallyfullofwater.YoucanexchangenumExchangeemptywaterbottlesfromthemarketwithonefullwaterbottle.Theoperationofdrinkingafullwaterbottleturnsitintoanemptybottle.......
  • Codility / LeetCode的重要性与注意事项
    Codility/Leetcode不只会针对回答内容给出最终分数,也会一并记录解题的过程供面试官参考;相较于现场考试,Codility/Leetcode可以省下更多时间,也能让求职者在最熟悉的环境发挥实力。 进行测验前先查看Codility/LeetcodeFAQ,并完成demo题。可试着多做几题练习题,能全部做......
  • 高频面试题 - 数据库优化方案
    一.考题再现最近很多小伙伴在跳槽面试,遇到了各种奇奇怪怪的问题。比如健哥的一个学生,在面试时被面试官问到如下问题:“我们做web开发都离不开http协议,那你了解http协议吗?”这时大家一般都是回答了解。然后面试官会接着对这个问题展开三连击,“Http协议是长连接还是短连接?......
  • [LeetCode] 2268. Minimum Number of Keypresses
    Youhaveakeypadwith 9 buttons,numberedfrom 1 to 9,eachmappedtolowercaseEnglishletters.Youcanchoosewhichcharacterseachbuttonismatchedtoaslongas:All26lowercaseEnglishlettersaremappedto.Eachcharacterismappedtoby exact......
  • [LeetCode] 2486. Append Characters to String to Make Subsequence
    Youaregiventwostrings s and t consistingofonlylowercaseEnglishletters.Return theminimumnumberofcharactersthatneedtobeappendedtotheendof s sothat t becomesa subsequence of s.A subsequence isastringthatcanbederived......
  • LeetCode 1201. Ugly Number III 数学+二分答案
    Anuglynumberisapositiveintegerthatisdivisibleby\(a\),\(b\),or\(c\).Givenfourintegers\(n\),\(a\),\(b\),and\(c\),returnthe\(n\)thuglynumber.Solution考虑如何二分答案,假设一个函数\(f(num,a,b,c)\)会得到\([1,num]\)中uglynumb......
  • [刷题记录Day4]Leetcode链表专题
    No.1题目两两交换链表中的节点思路模拟类型题目两个节点前后交换,同时记住原来的下一个节点虚拟头节点代码public ListNode swapPairs(ListNode head) { ListNode dummyHead = new ListNode(-1, head); ListNode cur = dummyHead; while (cur.next != ......
  • 《对线面试官》| 高频 Python 面试题 pt.1
    1.聊聊python中的值传递和引用传递吧值传递:值传递意味着在函数调用时,将实际参数的值复制一份传递给函数的形式参数在函数内部,形式参数将作为局部变量使用,对形式参数的修改不会影响原始变量的值引用传递引用传递意味着在函数调用时,将实际参数的引用(内存地址)传递给函数的......