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 79f2ab4 commit 635b515Copy full SHA for 635b515
top-k-frequent-elements/ys-han00.cpp
@@ -0,0 +1,28 @@
1
+#include <bits/stdc++.h>
2
+
3
+class Solution {
4
+public:
5
+ vector<int> topKFrequent(vector<int>& nums, int k) {
6
+ map<int, int> count;
7
8
+ for(int i = 0; i < nums.size(); i++) {
9
+ if(count.find(nums[i]) == count.end())
10
+ count[nums[i]] = 1;
11
+ else
12
+ count[nums[i]]++;
13
+ }
14
15
+ vector<pair<int, int>> cnt(count.begin(), count.end());
16
+ sort(cnt.begin(), cnt.end(), cmp);
17
18
+ vector<int> ans;
19
+ for(int i = 0; i < k; i++)
20
+ ans.push_back(cnt[i].first);
21
22
+ return ans;
23
24
25
+ static bool cmp(const pair<int, int>& a, const pair<int, int>& b) {
26
+ return a.second > b.second;
27
28
+};
0 commit comments