Skip to content

Commit 2d1d32d

Browse files
authored
Add files via upload
1 parent 6385b16 commit 2d1d32d

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
vector<int> frequencySort(vector<int>& nums) {
4+
map<int, int> mp;
5+
for (auto num : nums) {
6+
mp[num]++;
7+
}
8+
9+
vector<pair<int, int>> freq;
10+
for (auto x : mp) {
11+
freq.push_back(x);
12+
}
13+
// for (auto x : mp) {
14+
// cout << x.first << "," << x.second << "\n";
15+
// }
16+
sort(freq.begin(), freq.end(), [](const pair<int, int>& a, const pair<int, int>& b) {
17+
if (a.second == b.second) {
18+
return a.first > b.first;
19+
}
20+
return a.second < b.second;
21+
});
22+
23+
vector<int> ans;
24+
for (auto y : freq) {
25+
for (int x = 0 ; x < y.second; ++x) {
26+
ans.push_back(y.first);
27+
}
28+
}
29+
return ans;
30+
}
31+
};

0 commit comments

Comments
 (0)