Dashboard Temp Share Shortlinks Frames API

HTMLify

LeetCode - Sort Array by Increasing Frequency - Python
Views: 387 | Author: abh
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
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