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 b155433 commit 3337224Copy full SHA for 3337224
top-k-frequent-elements/crumbs22.cpp
@@ -0,0 +1,29 @@
1
+bool cmp(const pair<int, int>& a, const pair<int, int>& b)
2
+{
3
+ if (a.second == b.second)
4
+ return a.first < b.first;
5
+ return a.second > b.second;
6
+}
7
+
8
+class Solution {
9
+public:
10
+ vector<int> topKFrequent(vector<int>& nums, int k) {
11
+ unordered_map<int, int> umap;
12
13
+ for (int i = 0; i < nums.size(); i++)
14
+ {
15
+ auto tmp = umap.find(nums[i]);
16
+ if (tmp != umap.end())
17
+ tmp->second += 1;
18
+ else
19
+ umap.insert(make_pair(nums[i], 1));
20
+ }
21
+ vector<pair<int,int>> vec(umap.begin(), umap.end()); // map을 vector로 이동
22
+ sort(vec.begin(), vec.end(), cmp);
23
24
+ vector<int> result;
25
+ for (int i = 0; i < k; i++)
26
+ result.push_back(vec[i].first);
27
+ return (result);
28
29
+};
0 commit comments