-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path347.cpp
29 lines (29 loc) · 796 Bytes
/
347.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
vector<int>v;
int n=nums.size();
vector<vector<int>>ans(n+1);
unordered_map<int,int>m;
for(int i=0;i<n;i++)
m[nums[i]]++;
unordered_map<int,int>::iterator it;
for(it=m.begin();it!=m.end();it++)
ans[it->second].push_back(it->first);
for(int i=n;i>=0;i--)
{
if(!k)
return v;
if(ans[i].size())
{
int y=ans[i].size();
int x=min(k,y);
for(int j=0;j<x;j++)
v.push_back(ans[i][j]),k--;
}
}
return v;
}
};