Create 1636.cpp

This commit is contained in:
Kirigaya Kazuto 2022-09-19 08:56:32 +08:00 committed by GitHub
parent 607bf799c0
commit 915867e8f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

34
LeetCode/1636.cpp Normal file
View File

@ -0,0 +1,34 @@
class Solution {
public:
vector<int> frequencySort(vector<int>& nums) {
unordered_map<int, int> freq;
map<int, set<int>> keys;
int n = nums.size();
for (int i = 0; i < n; i++) {
auto iter = freq.find(nums[i]);
if (iter != freq.end()) {
keys[iter->second].erase(nums[i]);
iter->second++;
keys[iter->second].insert(nums[i]);
}
else {
freq.insert_or_assign(nums[i], 1);
keys[1].insert(nums[i]);
}
}
vector<int> result;
for (auto iter = keys.begin(); iter != keys.end(); iter++) {
for (auto xiter = iter->second.rbegin(); xiter != iter->second.rend(); xiter++) {
for (int i = 0; i < iter->first; i++) {
result.push_back(*xiter);
}
}
}
return result;
}
};