leetcode 324 Wiggle Sort 2
利用中位数的概念,中位数就是将一组数分成2等份(若为奇数,则中位数既不属于左也不属于右,所以是2等份),其一组数中任何一个元素都大于等于另一组数
那么我们是不是只要一左一右配合着插入,就保证了差值+-+-+-的要求?
由于题目输入限制了,必定存在解,所以此处我们不需要担心如果重复中位数太多,出现一左一右相等的情况
算法步骤:
1)找到中位数
利用lc215 Kth Largest...,将k=(nums.length+1) / 2,可以在o(n)内求出中位数
2)一左一右插入
0 2 4 .. 插大于中位数的
1 3 5 .. 插小于中位数的
考虑到中位数可能有重复的情况
我们从后往前插入小于中位数的值
从前往后插入大于中位数的值
剩下的空位补上中位数
代码中为了省事找中位数的部分用的o(klogn)的方法,详情可以看之前博客
class Solution {
public void wiggleSort(int[] nums) {
int median = findKthLargest(nums, (nums.length+1) / 2);
int left = 1;
int right = nums.length % 2 == 0 ? nums.length-2 : nums.length-1;
int[] tmp = new int[nums.length];
for(int i=0; i<nums.length; i++){
if(nums[i] > median){
tmp[left] = nums[i];
left += 2;
}else if(nums[i] < median){
tmp[right] = nums[i];
right -= 2;
}
}
while(left < nums.length){
tmp[left] = median;
left += 2;
}
while(right >= 0){
tmp[right] = median;
right -= 2;
}
System.arraycopy(tmp, 0, nums, 0, nums.length);
}
public int findKthLargest(int[] nums, int k) {
if(nums.length == 0)
return 0;
PriorityQueue<Integer> pq = new PriorityQueue<>(Comparator.reverseOrder());
int res = 0;
for(int num : nums)
pq.offer(num);
while(k != 0){
res = pq.poll();
k--;
}
return res;
}
}
leetcode 324 Wiggle Sort 2的更多相关文章
- [LeetCode] 324. Wiggle Sort II 摆动排序 II
Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...
- leetcode 280.Wiggle Sort 、324. Wiggle Sort II
Wiggle Sort: 注意:解法一是每次i增加2,题目不是保证3个3个的情况,而是整个数组都要满足要求. 解法一错误版本: 如果nums的长度是4,这种情况下nums[i+1]会越界.但是如果你用 ...
- LeetCode 280. Wiggle Sort (摆动排序)$
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- [LeetCode] 280. Wiggle Sort 摆动排序
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- Leetcode 280. Wiggle Sort
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- 324 Wiggle Sort II 摆动排序 II
给定一个无序的数组nums,将它重新排列成nums[0] < nums[1] > nums[2] < nums[3]...的顺序.例子:(1) 给定nums = [1, 5, 1, ...
- 324. Wiggle Sort II
这个题真是做得我想打人了.我生起气来连自己都打. 一开始quick select没啥可说的.但是in place把老命拼了都没想出来.. 看网上的答案是3 partition,MAP式子一看就由衷地反 ...
- LeetCode 280. Wiggle Sort C#
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- [LeetCode] Wiggle Sort II 摆动排序
Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...
随机推荐
- case in
#!/bin/bash source /etc/profilesource ~/.bashrc #自己定义$version_number case $version_number in3.0.17) ...
- 0926CSP-S模拟测试赛后总结
又一次垫底.持续低迷.20分. 赛时状态还可以.但是过于保守而不思进取.三道题目打了暴力就滚粗了. 暴力还挂掉了. T1暴力因为开小了数组挂成了0.1000的点,子序列个数我开了1e5以为足够了.结果 ...
- flink提交文件出现java.io.IOException:unable to close file because the last block does not have enough number of replicas异常
当提交已经打包好的jar包时候,控制台出现以下的错误.
- .NETFramework-Web.Mvc:HttpXxxAttribute-目录
ylbtech-.NETFramework-Web.Mvc:HttpXxxAttribute-目录 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返 ...
- POJ-2253-Frogger-/Floyd-Warshall/
Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sit ...
- 论文阅读-(ECCV 2018) Second-order Democratic Aggregation
本文是Tsung-Yu Lin大神所作(B-CNN一作),主要是探究了一种无序的池化方法\(\gamma\) -democratic aggregators,可以最小化干扰信息或者对二阶特征的内容均等 ...
- PropertyPlaceholderConfigurer的注意事项
1.基本的使用方法是<bean id="propertyConfigurerForWZ" class="org.springframework.beans.fact ...
- Carthage使用
# carthage 包管理 ## 安装过程 1) 安装homebrew ``` ruby$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githu ...
- 2019-5-21-Total-Commander-显示文件包含文件名扩展
title author date CreateTime categories Total Commander 显示文件包含文件名扩展 lindexi 2019-5-21 11:37:6 +0800 ...
- linux 服务 启动 关闭 列表
##查看服务在每个级别的运行状态 chkconfig --list httpd 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:启用 6:关闭 bluetooth ...