看板 Marginalman
2024-07-23 1636. Sort Array by Increasing Frequency Given an array of integers nums, sort the array in increasing order based on the frequency of the values. If multiple values have the same frequency, sort them in decreasing order. Return the sorted array. 額 大概就另外生個count 來當排序的第一 key 然後第二key 用 -的值 所以就姆咪姆咪 然後 匡噹 咚 class Solution { public: vector<int> frequencySort(vector<int>& nums) { map<int, int> freq; for (int c : nums) { freq[c]++; } vector<pair<int, int>> nums_to_sort; nums_to_sort.reserve(nums.size()); for (int c : nums) { nums_to_sort.push_back({freq[c], -c}); } sort(nums_to_sort.begin(), nums_to_sort.end(), less<>()); for (int i = 0; i < nums.size(); i++) { nums[i] = -nums_to_sort[i].second; } return nums; } }; -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 73.173.211.221 (美國) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1721705483.A.EF9.html
sustainer123: 大師 07/23 11:35