博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode: Wiggle Sort II
阅读量:5163 次
发布时间:2019-06-13

本文共 5225 字,大约阅读时间需要 17 分钟。

Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]....Example:(1) Given nums = [1, 5, 1, 1, 6, 4], one possible answer is [1, 4, 1, 5, 1, 6]. (2) Given nums = [1, 3, 2, 2, 3, 1], one possible answer is [2, 3, 1, 3, 1, 2].Note:You may assume all input has valid answer.Follow Up:Can you do it in O(n) time and/or in-place with O(1) extra space?

这道题应该是hard,首先思想上面参考了https://discuss.leetcode.com/topic/32861/3-lines-python-with-explanation-proof

I put the smaller half of the numbers on the even indexes and the larger half on the odd indexes, both from right to left:

Example nums = [1,2,...,7]      Example nums = [1,2,...,8] Small half:  4 . 3 . 2 . 1      Small half:  4 . 3 . 2 . 1 .Large half:  . 7 . 6 . 5 .      Large half:  . 8 . 7 . 6 . 5--------------------------      --------------------------Together:    4 7 3 6 2 5 1      Together:    4 8 3 7 2 6 1 5

I want:

  • Odd-index numbers are larger than their neighbors.

Since I put the larger numbers on the odd indexes, clearly I already have:

  • Odd-index numbers are larger than or equal to their neighbors.

Could they be "equal to"? That would require some number M to appear both in the smaller and the larger half. It would be the largest in the smaller half and the smallest in the larger half. Examples again, where S means some number smaller than M and L means some number larger than M.

Small half:  M . S . S . S      Small half:  M . S . S . S .Large half:  . L . L . M .      Large half:  . L . L . L . M--------------------------      --------------------------Together:    M L S L S M S      Together:    M L S L S L S M

You can see the two M are quite far apart. Of course M could appear more than just twice, for example:

Small half:  M . M . S . S      Small half:  M . S . S . S .Large half:  . L . L . M .      Large half:  . L . M . M . M--------------------------      --------------------------Together:    M L M L S M S      Together:    M L S M S M S M

You can see that with seven numbers, three M are no problem. And with eight numbers, four M are no problem. Should be easy to see that in general, with n numbers, floor(n/2) times M is no problem. Now, if there were more M than that, then my method would fail. But... it would also be impossible:

  • If n is even, then having more than n/2 times the same number clearly is unsolvable, because you'd have to put two of them next to each other, no matter how you arrange them.
  • If n is odd, then the only way to successfully arrange a number appearing more than floor(n/2) times is if it appears exactly floor(n/2)+1 times and you put them on all the even indexes. And to have the wiggle-property, all the other numbers would have to be larger. But then we wouldn't have an M in both the smaller and the larger half.

So if the input has a valid answer at all, then my code will find one.

 

然后根据https://discuss.leetcode.com/topic/41464/step-by-step-explanation-of-index-mapping-in-java

Assume your original array is {6,13,5,4,5,2}. After you get median element, the 'nums' is partially sorted such that the first half is larger or equal to the median, the second half is smaller or equal to the median, i.e

13   6   5   5   4   2         M

In the post , we have learned that , to get wiggle sort, you want to put the number in the following way such that

(1) elements smaller than the 'median' are put into the even slots(starting from right, left side may have left-overs, which will be stuffed by median)

(2) elements larger than the 'median' are put into the odd slots(starting from left, right side may have left-overs, which will be filled by median number)

(3) the medians are put into the remaining slots.

Index :       0   1   2   3 4 5 Small half: M S S Large half: L L M

M - Median, S-Small, L-Large. In this example, we want to put {13, 6, 5} in index 1,3,5 and {5,4,2} in index {0,2,4}

可惜每太看懂O(1)space的方法,我的方法用了O(N)space

1 public class Solution { 2     public void wiggleSort(int[] nums) { 3         int median = findKthLargest(nums, (nums.length+1)/2); 4         int odd = 1, even = (nums.length%2==0? nums.length-2 : nums.length-1); 5         int[] arr = new int[nums.length]; 6         for (int num : nums) { 7             if (num > median) { 8                 arr[odd] = num; 9                 odd += 2;10             }11             else if (num < median) {12                 arr[even] = num;13                 even -= 2;14             }15         }16         while (odd < arr.length) {17             arr[odd] = median;18             odd += 2;19         }20         while (even >= 0) {21             arr[even] = median;22             even -= 2;23         }24         for (int i=0; i
= nums[pivot]) {43 r--;44 }45 if (l == r) break;46 swap(nums, l, r);47 }48 swap(nums, l, pivot);49 if (l+1 == k) return nums[l];50 else if (l+1 < k) return findKthSmallest(nums, l+1, end, k);51 else return findKthSmallest(nums, start, l-1, k);52 }53 54 public void swap(int[] nums, int l, int r) {55 int temp = nums[l];56 nums[l] = nums[r];57 nums[r] = temp;58 }59 }

 

转载于:https://www.cnblogs.com/EdwardLiu/p/6181802.html

你可能感兴趣的文章
设计模式-策略模式(Strategy)
查看>>
django orm 数据查询详解
查看>>
JarvisOJ Basic 熟悉的声音
查看>>
C# list导出Excel(二)
查看>>
CAS 单点登录模块学习
查看>>
跟着辛星用PHP的反射机制来实现插件
查看>>
Android应用开发-网络编程①
查看>>
input中的name,value以及label中的for
查看>>
静态库制作-混编(工程是oc为基础)
查看>>
jQuery 显示加载更多
查看>>
代理模式
查看>>
Confluence 6 系统运行信息中的 JVM 内存使用情况
查看>>
Confluence 6 升级以后
查看>>
用JS实现版面拖拽效果
查看>>
二丶CSS
查看>>
《avascript 高级程序设计(第三版)》 ---第二章 在HTML中使用Javascript
查看>>
JS一些概念知识及参考链接
查看>>
TCP/IP协议原理与应用笔记24:网际协议(IP)之 IP协议的简介
查看>>
SAP HANA开发中常见问题- 基于SAP HANA平台的多团队产品研发
查看>>
游戏中的心理学(一):认知失调有前提条件
查看>>