[
sort
counter
]
BinarySearch 0088 Sort by Frequency and Value
Problem statement
https://binarysearch.com/problems/Sort-by-Frequency-and-Value/
Solution
Use counter and then sort values in given order, then reconstruct answer.
Complexity
It is O(n log n)
for time and O(n)
for space.
Code
class Solution:
def solve(self, nums):
cnt = sorted([(-y, -x) for x, y in Counter(nums).items()])
ans = []
for y, x in cnt:
ans += [-x]*(-y)
return ans