当前位置:网站首页>LeetCode 1636. Sort the array in ascending order by frequency
LeetCode 1636. Sort the array in ascending order by frequency
2022-07-06 16:43:00 【Daylight629】
1636. Sort the array in ascending order by frequency
Give you an array of integers nums
, Please arrange the array according to the frequency of each value Ascending Sort . If there are multiple values with the same frequency , Please put them according to the value itself Descending Sort .
Please return the sorted array .
Example 1:
Input :nums = [1,1,2,2,2,3]
Output :[3,1,1,2,2,2]
explain :'3' The frequency is 1,'1' The frequency is 2,'2' The frequency is 3 .
Example 2:
Input :nums = [2,3,1,3,2]
Output :[1,3,3,2,2]
explain :'2' and '3' The frequencies are 2 , So they are sorted in descending order according to the value itself .
Example 3:
Input :nums = [-1,1,-6,4,5,-6,1,4,1]
Output :[5,-1,4,4,-6,-6,1,1,1]
Tips :
1 <= nums.length <= 100
-100 <= nums[i] <= 100
Two 、 Method 1
Hash + Priority queue
class Solution {
public int[] frequencySort(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : nums) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
PriorityQueue<int[]> queue = new PriorityQueue<>((o1, o2)
-> o1[0] == o2[0] ? o2[1] - o1[1] : o1[0] - o2[0]
);
for (Map.Entry<Integer, Integer> entry: map.entrySet()) {
queue.add(new int[]{
entry.getValue(), entry.getKey()});
}
int[] res = new int[nums.length];
int idx = 0;
while (!queue.isEmpty()) {
int[] pos = queue.poll();
for (int i = 0; i< pos[0]; i++) {
res[idx++] = pos[1];
}
}
return res;
}
}
Complexity analysis
Time complexity :O(n).
Spatial complexity :O(n).
边栏推荐
- Market trend report, technical innovation and market forecast of tabletop dishwashers in China
- Codeforces Round #771 (Div. 2)
- Basic principles of video compression coding and audio compression coding
- QT implementation window gradually disappears qpropertyanimation+ progress bar
- Market trend report, technical innovation and market forecast of China's desktop capacitance meter
- One hundred questions of image processing (1-10)
- 拉取分支失败,fatal: ‘origin/xxx‘ is not a commit and a branch ‘xxx‘ cannot be created from it
- Tencent interview algorithm question
- Educational Codeforces Round 122 (Rated for Div. 2)
- Native JS realizes the functions of all selection and inverse selection -- Feng Hao's blog
猜你喜欢
第6章 Rebalance详解
第五章 Yarn资源调度器
QT realizes window topping, topping state switching, and multi window topping priority relationship
js封装数组反转的方法--冯浩的博客
软通乐学-js求字符串中字符串当中那个字符出现的次数多 -冯浩的博客
Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines
第5章 消费者组详解
Local visualization tools are connected to redis of Alibaba cloud CentOS server
Soft music -js find the number of times that character appears in the string - Feng Hao's blog
Chapter 7__ consumer_ offsets topic
随机推荐
原生js实现全选和反选的功能 --冯浩的博客
Simple records of business system migration from Oracle to opengauss database
Cmake error: could not create named generator visual studio 16 2019 solution
Pull branch failed, fatal: 'origin/xxx' is not a commit and a branch 'xxx' cannot be created from it
CMake速成
QT implementation window gradually disappears qpropertyanimation+ progress bar
< li> dot style list style type
Problem - 922D、Robot Vacuum Cleaner - Codeforces
ffmpeg命令行使用
第7章 __consumer_offsets topic
业务系统兼容数据库Oracle/PostgreSQL(openGauss)/MySQL的琐事
Spark independent cluster dynamic online and offline worker node
腾讯面试算法题
Hbuilder x format shortcut key settings
The concept of spark independent cluster worker and executor
Market trend report, technical innovation and market forecast of tabletop dishwashers in China
Acwing: the 56th weekly match
解决Intel12代酷睿CPU【小核载满,大核围观】的问题(WIN11)
Codeforces round 797 (Div. 3) no f
OneForAll安装使用