mirror of
https://github.com/Kiritow/OJ-Problems-Source.git
synced 2024-03-22 13:11:29 +08:00
Create 1636.cpp
This commit is contained in:
parent
607bf799c0
commit
915867e8f7
34
LeetCode/1636.cpp
Normal file
34
LeetCode/1636.cpp
Normal 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;
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue
Block a user