Given an array of integers nums, sort the array in increasing order based on the frequency of the values. If multiple values have the same frequency, sort them in decreasing order.
Return the sorted array.
Example 1:
Input: nums = [1,1,2,2,2,3] Output: [3,1,1,2,2,2]
Explanation: '3' has a frequency of 1, '1' has a frequency of 2, and '2' has a frequency of 3.
Solution: HashMap + Sort
Use a Map to store the counts of each elements, and then sort depends on the frequencies and values. The key point is define the customized sort function.
classSolution { public: vector<int> frequencySort(vector<int>& nums){ unordered_map<int, int> cnt; for (auto n : nums) { cnt[n]++; } sort(nums.begin(), nums.end(), [&cnt](int a, int b) { return (cnt[a] == cnt[b]) ? a > b : cnt[a] < cnt[b]; }); return nums; } };
Java
classSolution{ publicint[] frequencySort(int[] nums) { Integer[] arr = new Integer[nums.length]; for (int i = 0; i < arr.length; i++) { arr[i] = (Integer) nums[i]; } Map<Integer, Integer> map = new HashMap<>(); for(int i : nums) { map.put(i, map.getOrDefault(i, 0) + 1); } Arrays.sort(arr, new Comparator<Integer>() { @Override publicintcompare(Integer a, Integer b){ if(map.get(a).equals(map.get(b))) { return b - a; } return map.get(a) - map.get(b); } }); return Arrays.stream(arr).mapToInt(Integer::valueOf).toArray(); } }
Python
classSolution: deffrequencySort(self, nums: List[int]) -> List[int]: num_dict = {} for k in nums: if k notin num_dict: num_dict[k]=1 else: num_dict[k]=num_dict[k]+1
num_dict = sorted(num_dict.items(), key = lambda x:(x[1],-x[0])) res = [] for key,value in num_dict: res += [key]*value return res