当前位置:网站首页>LeetCode 324. Swing sort II
LeetCode 324. Swing sort II
2022-07-03 09:02:00 【Sasakihaise_】
【 Count sorting 】 because nums[i] The scope of 5000 Inside , So you can consider counting and sorting , Although need O(m) Spatial complexity of , But it can guarantee O(n) Time complexity of . The specific method is , Count the times of each occurrence first , From front to back, put the largest half element in the odd position , Then put the even position of the last large element from beginning to end .
class Solution {
// Count sorting 10:46 10:49
public void wiggleSort(int[] nums) {
int[] cnt = new int[5001];
for (var x: nums) {
cnt[x]++;
}
int j = 5000, n = nums.length;
for (var i = 1; i < n; i += 2) {
while (cnt[j] == 0) {
j--;
}
nums[i] = j;
cnt[j]--;
}
for (var i = 0; i < n; i += 2) {
while (cnt[j] == 0) {
j--;
}
nums[i] = j;
cnt[j]--;
}
}
}
边栏推荐
- Using variables in sed command
- AcWing 787. 归并排序(模板)
- Complex character + number pyramid
- 请求参数的发送和接收
- String splicing method in shell
- Common DOS commands
- Find the combination number acwing 886 Find the combination number II
- Binary tree sorting (C language, int type)
- 低代码前景可期,JNPF灵活易用,用智能定义新型办公模式
- SQL statement error of common bug caused by Excel cell content that is not paid attention to for a long time
猜你喜欢
LeetCode 535. TinyURL 的加密与解密
Memory search acwing 901 skiing
20220630 learning clock in
22-06-28 Xi'an redis (02) persistence mechanism, entry, transaction control, master-slave replication mechanism
On the setting of global variable position in C language
求组合数 AcWing 886. 求组合数 II
Binary tree traversal (first order traversal. Output results according to first order, middle order, and last order)
LeetCode 75. 颜色分类
Really explain the five data structures of redis
[concurrent programming] explicit lock and AQS
随机推荐
[rust note] 10 operator overloading
Common DOS commands
传统企业数字化转型需要经过哪几个阶段?
Low code momentum, this information management system development artifact, you deserve it!
樹形DP AcWing 285. 沒有上司的舞會
SQL statement error of common bug caused by Excel cell content that is not paid attention to for a long time
MySQL three logs
On the difference and connection between find and select in TP5 framework
22-05-26 西安 面试题(01)准备
cres
On a un nom en commun, maître XX.
22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
我们有个共同的名字,XX工
What is the difference between sudo apt install and sudo apt -get install?
Really explain the five data structures of redis
使用dlv分析golang进程cpu占用高问题
Gif remove blank frame frame number adjustment
Phpstudy 80 port occupied W10 system
干货!零售业智能化管理会遇到哪些问题?看懂这篇文章就够了
22-05-26 Xi'an interview question (01) preparation