The title

Given an array of integers, nums, sort the array in ascending order by the frequency of each value. If you have multiple values with the same frequency, sort them in descending order by the value itself.

Please return the sorted array.

 

Example 1: Input: nums = [1,1,2,2,2,3] Output: [3,1,1,2,2] Explanation: '3' frequency is 1, '1' frequency is 2, '2' frequency is 3. Example 2: input: nums = [2,3,1,3,2] output: [1,3,3,2,2] explanation: '2' and '3' are both frequencies of 2, so they are sorted in descending order by themselves. Example 3: input: nums = [- 1, 1, 6, four, five, or six,1,4,1] output: [5, 1,4,4, 6, 6,1,1,1] -Copy the code

Tip:

1 <= nums.length <= 100 -100 <= nums[i] <= 100

Their thinking

class Solution: def frequencySort(self, nums: List[int]) -> List[int]: Res = [] from collections import Counter # most_common, Counter = Counter(nums). Most_common () print(Counter) # sort(key=lambda x: (x[1], -x[0]) # print(counter) # print(counter) for val,count in counter: res += [val] * count # for _ in range(count): # res.append(val) # res.reverse() return res if __name__ == '__main__': ,1,2,2,2,3 # nums = [1] nums = [1, 1, 6, 4, 5, 6, 1, 4, 1] # nums = [2, 3, 1, 3, 2] # nums = [1,5,0,5] ret = Solution().frequencysort (num) print(ret)Copy the code