当前位置:网站首页>Daily question 5-1636 Sort arrays in ascending order by frequency
Daily question 5-1636 Sort arrays in ascending order by frequency
2022-06-22 10:58:00 【Programmed ape without hair loss 2】
subject :
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
Ideas :
Define a class first , Record the element value and frequency of each element , Use a list to store all the two tuples , And sort the list .
If the frequencies of two tuples are different , Sort by frequency in ascending order ;
If two tuples have the same frequency , Sort by element value in descending order .
Traverse the sorted list , For each binary in the list , Fill the element values into the sorted array according to the frequency . After traversal, you can get the complete sorted array .
java Code :
class Solution {
class Pair {
int num;
int freq;
public Pair(int num, int freq) {
this.num = num;
this.freq = freq;
}
}
public int[] frequencySort(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
for (int n : nums) {
map.put(n, map.getOrDefault(n, 0) + 1);
}
List<Pair> pairs = new ArrayList<>();
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
pairs.add(new Pair(entry.getKey(), entry.getValue()));
}
Collections.sort(pairs, (a, b) -> {
if (a.freq != b.freq) {
return a.freq - b.freq;
} else {
return b.num - a.num;
}
});
int[] sorted = new int[nums.length];
int index = 0;
for(Pair pair:pairs) {
int num = pair.num;
for(int i=0;i<pair.freq;i++) {
sorted[index++] = num;
}
}
return sorted;
}
}
边栏推荐
- QT compile the Internet of things management platform 36- communication protocol
- 一次特殊的文件上传
- heidisql插入记录,总是出错,要怎么改?
- Byte 3: do you know what Eureka is?
- npm install 命令的介绍
- scrapy. Transfer of meta parameter data of request()
- 普乐蛙VR台风逃生体验VR灾害自救模拟系统VR科普知识设备
- Reopen the terminal after NVM use or display the version before modification
- Native JS dynamically add and delete classes
- 微软 Edge 浏览器 Dev 104 发布,深 / 浅主题切换更加顺畅
猜你喜欢

2022-06-09 work record --yarn/npm-error-eperm: operation not permitted, UV_ cwd

一次特殊的文件上传

【这款工具配合jmeter,会让你的工作效率至少提升80%,强烈推荐给大家】

批量创建/删除文件、文件夹、修改文件名 后缀名

Go微服务(一)——RPC入门

Start from the principle of MVC and knock on an MVC framework to bring you the pleasure of being a great God

heidisql插入记录,总是出错,要怎么改?

SQL statement of final examination for College Students

普乐蛙5d飞行影院5d动感影院体验馆设备7d多人互动影院

线程常用调度方法
随机推荐
[Jenkins] shell script calls Jenkins API interface
TCP 3次握手的通俗理解
爱可可AI前沿推介(6.22)
npm install 命令的介绍
原生JS动态添加和删除类
嵌入式软件开发之程序架构设计-任务调度
Laravel 中类似 WordPress 的钩子和过滤器
Recommander un logiciel de machine virtuelle pour la construction rapide d'un Cluster d'ordinateurs à puce M1
高考生父亲深夜自述,最在意的不是孩子成绩,转折点一点都不假
【Shell】常用指令集锦
CVPR 2022 oral | a new motion oriented point cloud single target tracking paradigm
Byte 3: do you know what Eureka is?
大学生期末考试SQL语句
数据库课程虚拟教研室负责人杜小勇:立足国产数据库重大需求,探索课程体系建设新模式
The first "cyborg" in the world died, and he only transformed himself to "change his life against the sky"
PHP开发的网站,如何实现批量打印快递单的功能?
Opencv人脸识别之发送QQ邮箱
今天,SysAK 是如何实现业务抖动监控及诊断?&手把手带你体验Anolis OS|第25-26期
MySQL daily experience [02]
2022 Shaanxi Provincial Safety Officer C certificate examination question bank simulated examination platform operation