Wiggle Sort II
Problem
Given an integer array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3] >= nums[4] <= ...
- 1 ≤ nums.length ≤ 5 * 10⁴
- 0 ≤ nums[i] ≤ 5000
Example
nums = [1, 5, 1, 1, 6, 4][1, 6, 1, 5, 1, 4]The algorithm first sorts the array to get [1,1,1,4,5,6]. It then splits the sorted array into two halves: small = [1,1,1] and large = [4,5,6]. By placing the largest elements from small and large alternately starting from the end, it produces a wiggle pattern: indices 0,2,4 get values from small in descending order, and indices 1,3,5 get values from large in descending order. This ensures nums[0] <= nums[1] >= nums[2] <= nums[3] >= nums[4] <= nums[5].
Approach
Straightforward Solution
A brute-force approach would attempt to reorder elements by swapping adjacent pairs repeatedly until the wiggle condition is met, which can be inefficient and complex to implement correctly.
Core Observation
The wiggle property requires alternating elements to be less than or equal to and greater than or equal to their neighbors. Sorting the array reveals the relative order of elements, enabling a controlled interleaving of smaller and larger halves to satisfy the wiggle condition.
Path to Optimal
Sorting the array provides a clear ordering. Splitting into two halves and then placing elements from the smaller half at even indices and from the larger half at odd indices, both in descending order, guarantees the wiggle property. This approach avoids complex swaps and ensures correctness by construction.
Optimal Approach
Sort the array, split it into two halves (small and large), then fill the original array by placing elements from the end of small at even indices and from the end of large at odd indices. This interleaving ensures the wiggle pattern is maintained throughout the array.
Time
O(n log n)
Sorting the array dominates the runtime with O(n log n). The subsequent interleaving is a single pass O(n) operation.
Space
O(n)
Auxiliary space is used to store the sorted copy and the two halves. The in-place rearrangement modifies the original array but requires O(n) extra space for the sorted arrays.
Pattern Spotlight
Sorting with Controlled Interleaving
Sorting reveals the global order, and by splitting and interleaving halves in reverse order, the wiggle property is guaranteed without complex swaps or multiple passes.
Solution
| 1 | class Solution: |
| 2 | def wiggleSort(self, nums: list[int]) -> None: |
| 3 | sorted_nums = sorted(nums) |
| 4 | mid = (len(nums) + 1) // 2 |
| 5 | |
| 6 | small = sorted_nums[:mid] |
| 7 | large = sorted_nums[mid:] |
| 8 | |
| 9 | small_i = len(small) - 1 |
| 10 | large_i = len(large) - 1 |
| 11 | |
| 12 | for i in range(len(nums)): |
| 13 | if i % 2 == 0: |
| 14 | nums[i] = small[small_i] |
| 15 | small_i -= 1 |
| 16 | else: |
| 17 | nums[i] = large[large_i] |
| 18 | large_i -= 1 |
Step-by-Step Solution
Sort and Partition the Array into Small and Large Halves
| 3 | sorted_nums = sorted(nums) |
| 4 | mid = (len(nums) + 1) // 2 |
| 6 | small = sorted_nums[:mid] |
| 7 | large = sorted_nums[mid:] |
Objective
To create two sorted subarrays representing the smaller and larger halves of the original array.
Key Insight
Sorting the array reveals the global order of elements. Splitting it at the midpoint ensures the smaller half contains elements that are less than or equal to those in the larger half. This partitioning is essential to interleave elements to satisfy the wiggle property.
Interview Quick-Check
Core Logic
Sorting and splitting the array into two halves allows controlled interleaving of smaller and larger elements to enforce the wiggle pattern.
Common Pitfalls & Bugs
Incorrectly calculating the midpoint or splitting the array can break the wiggle property, especially for arrays with odd length.
Interleave Elements from Small and Large Halves in Reverse Order
| 9 | small_i = len(small) - 1 |
| 10 | large_i = len(large) - 1 |
| 12 | for i in range(len(nums)): |
| 13 | if i % 2 == 0: |
| 14 | nums[i] = small[small_i] |
| 15 | small_i -= 1 |
| 16 | else: |
| 17 | nums[i] = large[large_i] |
| 18 | large_i -= 1 |
Objective
To fill the original array by placing elements from the small half at even indices and from the large half at odd indices, both starting from their ends.
Key Insight
By placing elements from the end of each half, the largest elements in each half are placed first, ensuring that the wiggle condition is met. This reverse interleaving prevents adjacent elements from violating the alternating inequality pattern.
Interview Quick-Check
Core Logic
Filling even indices with the largest elements from the smaller half and odd indices with the largest elements from the larger half guarantees nums[even] <= nums[odd] >= nums[even+2].
State & Boundaries
The loop runs through all indices, ensuring every element is assigned correctly without overlap or omission.
Common Pitfalls & Bugs
Failing to decrement indices after assignment or mixing the order of placement can break the wiggle property.
Line Analysis
This solution has 3 Critical lines interviewers watch for.
sorted_nums = sorted(nums)
Create a sorted copy of the input array.
Sorting reveals the global order of elements, which is essential for partitioning and controlled interleaving to achieve the wiggle pattern.
nums[i] = small[small_i]
Assign the current even index with the element from the smaller half at small_i.
Placing the largest remaining element from the smaller half at even indices ensures nums[even] <= nums[odd] in the final array.
nums[i] = large[large_i]
Assign the current odd index with the element from the larger half at large_i.
Placing the largest remaining element from the larger half at odd indices ensures nums[odd] >= nums[even] in the final array.
mid = (len(nums) + 1) // 2
Calculate the midpoint index for splitting the array.
The midpoint divides the array into two halves, ensuring the smaller half contains the lower values and the larger half contains the higher values, which is critical for the wiggle property.
small_i = len(small) - 1
Initialize index pointer for the smaller half starting from its end.
Starting from the end ensures the largest elements in the smaller half are placed first, maintaining the wiggle property when interleaved.
large_i = len(large) - 1
Initialize index pointer for the larger half starting from its end.
Starting from the end ensures the largest elements in the larger half are placed first, maintaining the wiggle property when interleaved.
small = sorted_nums[:mid]
Extract the smaller half of the sorted array.
The smaller half will be placed at even indices to ensure these positions hold smaller or equal values compared to the adjacent odd indices.
large = sorted_nums[mid:]
Extract the larger half of the sorted array.
The larger half will be placed at odd indices to ensure these positions hold larger or equal values compared to adjacent even indices.
if i % 2 == 0:
Check if the current index is even.
Even indices are designated to receive elements from the smaller half to satisfy the wiggle condition.
small_i -= 1
Decrement the pointer for the smaller half after assignment.
Moving backward through the smaller half ensures elements are placed in descending order, which is critical for maintaining the wiggle property.
else:
Handle the case when the current index is odd.
Odd indices are designated to receive elements from the larger half to satisfy the wiggle condition.
large_i -= 1
Decrement the pointer for the larger half after assignment.
Moving backward through the larger half ensures elements are placed in descending order, which is critical for maintaining the wiggle property.
for i in range(len(nums)):
Iterate over each index of the original array.
This loop ensures every position in the original array is assigned a value from either the smaller or larger half, preserving the wiggle pattern.
Test Your Understanding
Why does placing elements from the smaller half at even indices and from the larger half at odd indices, both in descending order, guarantee the wiggle property?
Because the smaller half elements placed at even indices are always less than or equal to the larger half elements placed at adjacent odd indices, and by placing them in descending order from the end, the alternating inequality pattern nums[0] <= nums[1] >= nums[2] <= nums[3] ... is maintained throughout.
Related Problems
Sorting pattern
Don't just read it. Drill it.
Reconstruct Wiggle Sort II from memory until it sticks. AlgoDrill blanks out key lines and makes you fill them back in, step by step.
Unlock the Wiggle Sort II drill