Skip to content

Commit 3c73682

Browse files
committed
Solution as on 11-04-2022 17:14 pm
1 parent bb12194 commit 3c73682

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

0347. Top K Frequent Elements.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// 347. Top K Frequent Elements347. Top K Frequent Elements
2+
3+
class Solution
4+
{
5+
public:
6+
vector<int> topKFrequent(vector<int> &nums, int k)
7+
{
8+
unordered_map<int, int> mp;
9+
10+
for (auto i : nums)
11+
++mp[i];
12+
13+
priority_queue<pair<int, int>> pq;
14+
vector<int> ans;
15+
for (auto i = mp.begin(); i != mp.end(); ++i)
16+
{
17+
pq.push(make_pair(i->second, i->first));
18+
}
19+
20+
while (k--)
21+
{
22+
ans.push_back(pq.top().second);
23+
pq.pop();
24+
}
25+
return ans;
26+
}
27+
};

0 commit comments

Comments
 (0)