HTMLify

LeetCode - Sort Array by Increasing Frequency - Python
Views: 25 | Author: abh
class Solution:
    def frequencySort(self, nums: List[int]) -> List[int]:
        freq = []
        for n in set(nums):
            freq.append((n, nums.count(n)))
        freq = sorted(freq, key=lambda e:e[1])        
        ans = []
        f = 1
        while len(ans) != len(nums):
            ns = filter(lambda e:e[1]==f, freq)
            for n in sorted(ns, reverse=True):
                ans += [n[0]]*f
            f += 1
        return ans

Comments