0280. Wiggle Sort
https://leetcode.com/problems/wiggle-sort
Description
Given an integer array nums
, reorder it such that nums[0] <= nums[1] >= nums[2] <= nums[3]...
.
You may assume the input array always has a valid answer.
Example 1:
**Input:** nums = [3,5,2,1,6,4]
**Output:** [3,5,1,6,2,4]
**Explanation:** [1,6,2,5,3,4] is also accepted.
Example 2:
**Input:** nums = [6,6,5,6,3,8]
**Output:** [6,6,5,6,3,8]
Constraints:
1 <= nums.length <= 5 * 104
0 <= nums[i] <= 104
It is guaranteed that there will be an answer for the given input
nums
.
Follow up: Could you do it without sorting the array?
ac1: in-place odd/even check
This is kinda "aha" brain teaser. See proof: https://leetcode.com/problems/wiggle-sort/discuss/71693/My-explanations-of-the-best-voted-Algo/112986.
class Solution {
public void wiggleSort(int[] nums) {
// edge cases
if (nums.length < 2) return;
// body
for (int i = 0; i < nums.length - 1; i++) {
if (i % 2 == 0 && nums[i] > nums[i+1]) swap(nums, i, i+1);
if (i % 2 == 1 && nums[i] < nums[i+1]) swap(nums, i, i+1);
}
}
private void swap(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
}
// O(N) time, O(1) space
ac2: quickselect partition
Same as 324-Wiggle Sort II
Last updated
Was this helpful?