We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 88165ec commit 7785c45Copy full SHA for 7785c45
top-k-frequent-elements/oyeong011.cpp
@@ -1,21 +1,16 @@
1
class Solution {
2
public:
3
- int longestConsecutive(vector<int>& nums) {
4
- const int INF = 987654321;
5
- int temp = INF, ret = 0, cur = 0;
+ vector<int> topKFrequent(vector<int>& nums, int k) {
+ map<int, int> mp;
+ priority_queue<pair<int, int>> pq;
6
+ vector<int> ans;
7
- sort(nums.begin(), nums.end());
8
- for(int a : nums){
9
- if(a == temp)continue;
10
- if(temp == INF || temp + 1 == a){
11
- cur++; temp = a;
12
- } else {
13
- ret = max(ret, cur);
14
- cur = 1;
15
- temp = a;
16
- }
17
18
19
- return ret;
+ for(auto b : nums) mp[b]++;
+
+ for(auto p : mp) pq.push({p.second, p.first});
+ while(k--)ans.push_back(pq.top().second), pq.pop();
+ return ans;
20
}
21
};
0 commit comments