当前位置:网站首页>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]--;
}
}
}边栏推荐
- [concurrent programming] working mechanism and type of thread pool
- Slice and index of array with data type
- createjs easeljs
- MySQL three logs
- 【Rust 笔记】10-操作符重载
- Monotonic stack -503 Next bigger Element II
- Graphics_ Games101/202 learning notes
- Unity editor expansion - scrolling list
- PHP uses foreach to get a value in a two-dimensional associative array (with instances)
- How to delete CSDN after sending a wrong blog? How to operate quickly
猜你喜欢
![[redis] redis persistent RDB vs AOF (source code)](/img/57/b6a86c49cedee31fc00dc5d1372023.jpg)
[redis] redis persistent RDB vs AOF (source code)

请求参数的发送和接收

樹形DP AcWing 285. 沒有上司的舞會

Concurrent programming (VI) ABA problems and solutions under CAS

too many open files解决方案

Sending and receiving of request parameters

Dom4j遍历和更新XML

Find the combination number acwing 885 Find the combination number I
![[RPC] RPC remote procedure call](/img/dc/872204ea47fcff04cdb72e18a2a4ef.jpg)
[RPC] RPC remote procedure call

Debug debugging - Visual Studio 2022
随机推荐
cres
Unity editor expansion - scrolling list
TP5 multi condition sorting
Annotations simplify configuration and loading at startup
Try to reprint an article about CSDN reprint
DOM 渲染系统(render mount patch)响应式系统
UE4 source code reading_ Bone model and animation system_ Animation node
Markdown learning
Concurrent programming (VI) ABA problems and solutions under CAS
Unity editor expansion - the design idea of imgui
UE4 source code reading_ Mobile synchronization
Graphics_ Learnopongl learning notes
22-06-28 Xi'an redis (02) persistence mechanism, entry, transaction control, master-slave replication mechanism
状态压缩DP AcWing 291. 蒙德里安的梦想
求组合数 AcWing 885. 求组合数 I
Monotonic stack -42 Connect rainwater
Message queue for interprocess communication
二进制转十进制,十进制转二进制
Really explain the five data structures of redis
Solution of 300ms delay of mobile phone