当前位置:网站首页>LeetCode 324. 摆动排序 II
LeetCode 324. 摆动排序 II
2022-07-03 08:49:00 【Sasakihaise_】
【计数排序】因为nums[i]的范围在5000内,所以可以考虑计数排序,虽然需要O(m)的空间复杂度,但是可以保证O(n)的时间复杂度。具体做法就是,先统计每个出现的次数,从前往后先把奇数位置放上最大的一半元素,然后从头到尾再把偶数位置放上次大的元素。
class Solution {
// 计数排序 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]--;
}
}
}
边栏推荐
- 22-05-26 Xi'an interview question (01) preparation
- DOM render mount patch responsive system
- Annotations simplify configuration and loading at startup
- [rust notes] 11 practical features
- Common DOS commands
- 22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
- Unity Editor Extension - drag and drop
- cres
- Unity interactive water ripple post-treatment
- Talking about: is the HashSet set ordered or disordered /hashset set unique, why can we store elements with the same content
猜你喜欢
[set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q
【Rust笔记】02-所有权
Unity editor expansion - draw lines
Gaussian elimination acwing 883 Gauss elimination for solving linear equations
Message queue for interprocess communication
[rust notes] 02 ownership
Binary to decimal, decimal to binary
JS non Boolean operation - learning notes
Really explain the five data structures of redis
单调栈-503. 下一个更大元素 II
随机推荐
记忆化搜索 AcWing 901. 滑雪
【Rust 笔记】09-特型与泛型
[rust notes] 08 enumeration and mode
Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
Annotations simplify configuration and loading at startup
Gif remove blank frame frame number adjustment
LinkedList set
Apache startup failed phpstudy Apache startup failed
20220630学习打卡
Unity learning notes
Markdown learning
【Rust 笔记】12-闭包
C language file reading and writing
Redis cluster series 4
[concurrent programming] atomic operation CAS
Six dimensional space (C language)
[set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q
UE4 source code reading_ Mobile synchronization
How to place the parameters of the controller in the view after encountering the input textarea tag in the TP framework
求组合数 AcWing 886. 求组合数 II