class Solution:
def wiggleSort(self, nums):
nums.sort()
half = (len(nums) + 1) // 2
small = nums[:half][::-1]
large = nums[half:][::-1]
for i in range(len(nums)):
if i % 2 == 0:
nums[i] = small[i // 2]
else:
nums[i] = large[i // 2]
class Solution {
public:
void wiggleSort(vector<int>& nums) {
vector<int> sorted(nums);
sort(sorted.begin(), sorted.end());
int n = nums.size();
int half = (n + 1) / 2;
int j = half - 1, k = n - 1;
for (int i = 0; i < n; ++i) {
if (i % 2 == 0)
nums[i] = sorted[j--];
else
nums[i] = sorted[k--];
}
}
};
class Solution {
public void wiggleSort(int[] nums) {
int[] sorted = nums.clone();
Arrays.sort(sorted);
int n = nums.length, half = (n + 1) / 2;
int j = half - 1, k = n - 1;
for (int i = 0; i < n; ++i) {
if (i % 2 == 0)
nums[i] = sorted[j--];
else
nums[i] = sorted[k--];
}
}
}
var wiggleSort = function(nums) {
let sorted = [...nums].sort((a, b) => a - b);
let n = nums.length;
let half = Math.floor((n + 1) / 2);
let small = sorted.slice(0, half).reverse();
let large = sorted.slice(half).reverse();
for (let i = 0; i < n; ++i) {
nums[i] = (i % 2 === 0) ? small[Math.floor(i / 2)] : large[Math.floor(i / 2)];
}
};
The Wiggle Sort problem asks you to reorder a given integer array nums
so that it follows a specific pattern:
nums[0] < nums[1] > nums[2] < nums[3] > nums[4] ...
That is, every even-indexed element should be less than its next neighbor, and every odd-indexed element should be greater than its next neighbor (if one exists). You must rearrange the elements in place, using all original numbers exactly once, and there is at least one valid solution for every input.
The first idea that may come to mind is to try all possible permutations of the array and check if any of them satisfy the wiggle property. However, this brute-force approach is impractical because the number of permutations grows factorially with the array size.
Instead, we want a more efficient method. If we sort the array, we can try to interleave the smaller and larger halves to create the desired up-and-down pattern. By carefully choosing which elements go into even and odd positions, we can ensure the wiggle property is maintained. This shifts our thinking from "trying everything" to "systematically constructing a valid order."
This approach ensures that the wiggle property is maintained, even with duplicate elements, and does not require extra space beyond a copy of the sorted array.
Let's take nums = [1, 5, 1, 1, 6, 4]
as an example:
[1, 1, 1, 4, 5, 6]
[1, 1, 1]
[4, 5, 6]
[1, 1, 1]
[6, 5, 4]
[1, 6, 1, 5, 1, 4]
The result satisfies the wiggle condition at every position.
This is a significant improvement over the brute-force approach and is efficient enough for large inputs.
The Wiggle Sort problem can be solved efficiently by sorting the array, splitting it into two halves, reversing them, and then interleaving the elements into even and odd positions. This strategy leverages the properties of sorted order and careful assignment to guarantee the wiggle pattern, even when duplicate values are present. The result is a simple, elegant, and efficient solution that avoids brute-force and works for all valid input arrays.