-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1636.cpp
More file actions
27 lines (25 loc) · 789 Bytes
/
1636.cpp
File metadata and controls
27 lines (25 loc) · 789 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public:
static bool compare(const pair<int, int> p1, const pair<int, int> p2) {
if (p1.first != p2.first) return p1.first < p2.first;
return p1.second > p2.second;
}
vector<int> frequencySort(vector<int>& nums) {
unordered_map<int, int> mp;
for (auto& num : nums) mp[num]++;
// freq, value
vector<pair<int, int>> v;
for (auto& element : mp) {
v.push_back(make_pair(element.second, element.first));
}
sort(v.begin(), v.end(), compare);
int index = 0;
for (auto& [freq, value] : v) {
for (int i = 0; i < freq; ++i) {
nums[index] = value;
index++;
}
}
return nums;
}
};