From 915867e8f767be73dcea457fe1643fa19f60e90c Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Mon, 19 Sep 2022 08:56:32 +0800 Subject: [PATCH] Create 1636.cpp --- LeetCode/1636.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 LeetCode/1636.cpp diff --git a/LeetCode/1636.cpp b/LeetCode/1636.cpp new file mode 100644 index 0000000..975d100 --- /dev/null +++ b/LeetCode/1636.cpp @@ -0,0 +1,34 @@ + +class Solution { +public: + vector frequencySort(vector& nums) { + unordered_map freq; + map> 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 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; + } +};