Re: [闲聊] 每日leetcode

楼主: smart0eddie (smart0eddie)   2024-07-23 11:31:21
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;
}
};
作者: sustainer123 (caster)   2024-07-23 11:35:00
大师

Links booklink

Contact Us: admin [ a t ] ucptt.com