Leetcode:wiggle Sort II

Source: Internet
Author: User

Given an unsorted array nums, reorder it such, 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 does it in O (n) Time and /or in-place with O (1) extra space?

This problem should be hard, first of all the ideas above refer to 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 is larger than their neighbors.

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

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

Could They is "equal to"? That would require some number M to appear both in the smaller and the larger half. It would is 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 both M is 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 the with seven numbers, three M is no problem. And with eight numbers, four M is no problem. should be easy-to-see, the With N numbers, floor (N/2) times M-is no problem. Now, if there were more M than that and then my method would fail. But ... it would also be impossible:

    • If N is even and then has more than N/2 times the same number clearly are unsolvable, because you ' d has to put in the them Next to all other, no matter how do you arrange them.
    • If n is odd and then the only-to successfully arrange a number appearing more than floor (N/2) times is if it appears exac tly Floor (N/2) +1 times and your put them on all the even indexes. and to having the wiggle-property, all of the other numbers would has to be larger. But then we wouldn ' t has a M in both the smaller and the larger half.

So if the input have a valid answer at all and then my code would find one.

And then according to 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 your get median element, the ' nums ' is partially sorted such the first half are larger or equal to the median, th E second half is smaller or equal to the median, i.e

13   6   5   5   4   2         M

In the post Https://leetcode.com/discuss/76965/3-lines-python-with-explanation-proof, we had learned that, to get WIGGL E sort, you want-put the number in the following-such that

(1) elements smaller than the ' median ' is put into the even slots (starting from right, left side could have left-overs, whi Ch'll be stuffed by median)

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

(3) The medians is put into the remaining slots.

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

M-median, S-small, L-large. In the example, we want to put {6, 5} at index 1,3,5 and {5,4,2} in index {0,2,4}

Unfortunately every way to understand the O (1) space, my method uses O (N) space

1  Public classSolution {2      Public voidWigglesort (int[] nums) {3         intMedian = Findkthlargest (Nums, (nums.length+1)/2);4         intOdd = 1, even = (nums.length%2==0? nums.length-2: Nums.length-1);5         int[] arr =New int[nums.length];6          for(intnum:nums) {7             if(Num >median) {8Arr[odd] =num;9Odd + = 2;Ten             } One             Else if(Num <median) { AArr[even] =num; -Even-= 2; -             } the         } -          while(Odd <arr.length) { -Arr[odd] =median; -Odd + = 2; +         } -          while(Even >= 0) { +Arr[even] =median; AEven-= 2; at         } -          for(inti=0; i<arr.length; i++) { -Nums[i] =Arr[i]; -         } -     } -      in      Public intFindkthlargest (int[] Nums,intk) { -         intLen =nums.length; to         returnFindkthsmallest (nums, 0, len-1, len-k+1); +     } -      the      Public intFindkthsmallest (int[] Nums,intStartintEndintk) { *         intL =start; $         intR =end;Panax Notoginseng         intPivot =end; -          while(L <r) { the              while(L<r && Nums[l] <Nums[pivot]) { +l++; A             } the              while(L<r && Nums[r] >=Nums[pivot]) { +r--; -             } $             if(L = = r) Break; $ Swap (Nums, L, R); -         } - Swap (nums, L, pivot); the         if(l+1 = = k)returnNums[l]; -         Else if(L+1 < k)returnFindkthsmallest (Nums, l+1, end, k);Wuyi         Else returnFindkthsmallest (Nums, start, l-1, k); the     } -      Wu      Public voidSwapint[] Nums,intLintr) { -         inttemp =Nums[l]; AboutNUMS[L] =Nums[r]; $NUMS[R] =temp; -     } -}

Leetcode:wiggle Sort II

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.