当前位置:网站首页>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 double-sided foam tape in China
- Bidirectional linked list - all operations
- SF smart logistics Campus Technology Challenge (no T4)
- Browser print margin, default / borderless, full 1 page A4
- JS encapsulates the method of array inversion -- Feng Hao's blog
- (lightoj - 1236) pairs forming LCM (prime unique decomposition theorem)
- Chapter 5 namenode and secondarynamenode
- Li Kou: the 81st biweekly match
- 两个礼拜速成软考中级软件设计师经验
猜你喜欢
两个礼拜速成软考中级软件设计师经验
Discussion on QWidget code setting style sheet
Li Kou - 298th weekly match
拉取分支失败,fatal: ‘origin/xxx‘ is not a commit and a branch ‘xxx‘ cannot be created from it
第7章 __consumer_offsets topic
Mp4 format details
Anaconda下安装Jupyter notebook
MariaDB的安装与配置
Kubernetes cluster deployment
VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题
随机推荐
Effet d'utilisation, déclenché lorsque les composants de la fonction sont montés et déchargés
OneForAll安装使用
Hbuilder x format shortcut key settings
Tert butyl hydroquinone (TBHQ) Industry Research Report - market status analysis and development prospect forecast
Pull branch failed, fatal: 'origin/xxx' is not a commit and a branch 'xxx' cannot be created from it
Chapter 2 shell operation of hfds
(lightoj - 1349) Aladdin and the optimal invitation (greed)
音视频开发面试题
< li> dot style list style type
The concept of spark independent cluster worker and executor
Base dice (dynamic programming + matrix fast power)
Input can only input numbers, limited input
Li Kou - 298th weekly match
<li>圆点样式 list-style-type
FLV格式详解
js封装数组反转的方法--冯浩的博客
Li Kou: the 81st biweekly match
原生js实现全选和反选的功能 --冯浩的博客
Chapter 5 namenode and secondarynamenode
Specify the format time, and fill in zero before the month and days