Skip to content
LC-1636 Easy LeetCode

1636. Sort Array by Increasing Frequency

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 80% Topics: Array, Hash Table, Sorting
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(nlogn)
# Space: O(n)

import collections


class Solution(object):
    def frequencySort(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        count = collections.Counter(nums)
        return sorted(nums, key=lambda x: (count[x], -x))

Solution from kamyu104/LeetCode-Solutions · MIT