🔢💯 Ace Frequency Sorting! 🚀✨ Simple & Effective Python Solution! 🎓🔥
Intuition\n Describe your first thoughts on how to solve this problem. \nTo sort elements of an array based on their frequency and then by their value in decrea
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo sort elements of an array based on their frequency and then by their value in decreasing order if frequencies are the same, we can use a frequency dictionary to keep track of how often each number appears. Then, we can use this information to construct the sorted array as required.\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nCount Frequencies: Use a dictionary to count the frequency of each element in the input list.\nGroup by Frequency: Use another dictionary to group elements by their frequencies.\nSort by Frequency and Value:\nFirst, sort the elements by their frequency in ascending order.\nIf two elements have the same frequency, sort them by their value in descending order.\nConstruct Result: Based on the sorted frequency groups, construct the result list.\n# Complexity\n- Time complexity:O(nlogn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution(object):\n    def frequencySort(self, nums):\n        """\n        :type nums: List[int]\n        :rtype: List[int]\n        """\n        frequencyDict, numDict = {},  {}\n        ret = []\n\n        for num in nums:\n            if num in numDict:\n                numDict[num] += 1\n            else:\n                numDict[num] = 1\n\n        for key, val in numDict.items():\n            if val in frequencyDict:\n                frequencyDict[val] += [key]\n            else:\n                frequencyDict[val] = [key]\n\n        frequencyDict = sorted(frequencyDict.items())\n        for item in frequencyDict:\n            [frequency, numList] = item\n            numList.sort(reverse=True)\n            for num in numList:\n                temp = [num] * frequency\n                ret += temp\n        return ret\n```\n\n![packied_spongebob.jpg](https://assets.leetcode.com/users/images/3d1a5355-c3b0-4561-bc1a-b4dc0d89adb1_1721743798.6866136.jpeg)\n
['Python']