280. Wiggle Sort
Given an unsorted arraynums
, reorder it in-place such thatnums[0] <= nums[1] >= nums[2] <= nums[3]...
.
Example:
Thoughts:
Naive: traverse through the element (sort each two element (from [i, i + 2) according to alternative order (ascending vs decending)
Window of 2: always ranking nums[i-1] and num[i] with the alternating order: when i is odd, then nums[i-1] should be <= nums[i]; when i is even (Excepet 0), nums[i-1] should be >= nums[i]
Code
Code
Last updated
Was this helpful?