{x}
blog image

Wiggle Sort II

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 = [1,5,1,1,6,4]
Output: [1,6,1,5,1,4]
Explanation: [1,4,1,5,1,6] is also accepted.

Example 2:

Input: nums = [1,3,2,2,3,1]
Output: [2,3,1,3,1,2]

 

Constraints:

  • 1 <= nums.length <= 5 * 104
  • 0 <= nums[i] <= 5000
  • It is guaranteed that there will be an answer for the given input nums.

 

Follow Up: Can you do it in O(n) time and/or in-place with O(1) extra space?

Solution Explanation for Wiggle Sort II

This problem asks to reorder an integer array nums such that nums[0] < nums[1] > nums[2] < nums[3].... The solutions presented utilize different approaches to achieve this.

Approach 1: Sorting and Interleaving

This approach is straightforward and efficient. It involves:

  1. Sorting: First, sort the input array nums to obtain a sorted array arr. This step has a time complexity of O(n log n) where n is the length of the array.

  2. Interleaving: Next, we create the wiggle pattern by interleaving elements from the sorted array. We use two pointers, i and j, initialized to the middle element and the last element of the sorted array, respectively. Then, we iterate through the original nums array. For even indices, we assign values from arr[i] (moving from middle towards the beginning) and for odd indices, we assign values from arr[j] (moving from the end towards the middle). This ensures the alternating greater-than and less-than pattern. This interleaving step takes O(n) time.

Time Complexity: O(n log n) due to sorting. The interleaving step is linear. Space Complexity: O(n) because we create a copy of the array for sorting (in-place sorting can reduce this to O(1)).

Approach 2: Counting Sort and Interleaving

This approach improves the time complexity for certain scenarios. It leverages the constraint that the values are within a limited range (0 to 5000).

  1. Counting Sort: It uses counting sort to count the occurrences of each number in the input array. Counting sort has a time complexity of O(n + k) where k is the range of values (5001 in this case).

  2. Interleaving: Similar to Approach 1, we interleave the numbers based on the counts. We start from the largest number and assign elements to even and odd indices in the original array, decreasing the count for each number as we use it. This step is also linear, O(n).

Time Complexity: O(n + k) which in this specific problem becomes O(n) since k is a constant. This is asymptotically faster than Approach 1's O(n log n) time complexity. Space Complexity: O(k) which is O(1) in the context of this problem, as k is constant. The space used by the bucket array is constant.

Code Examples (Python)

Approach 1:

class Solution:
    def wiggleSort(self, nums: List[int]) -> None:
        arr = sorted(nums)
        n = len(arr)
        i, j = (n - 1) >> 1, n - 1
        for k in range(n):
            if k % 2 == 0:
                nums[k] = arr[i]
                i -= 1
            else:
                nums[k] = arr[j]
                j -= 1
 

Approach 2:

class Solution:
    def wiggleSort(self, nums: List[int]) -> None:
        bucket = [0] * 5001
        for v in nums:
            bucket[v] += 1
        n = len(nums)
        j = 5000
        for i in range(1, n, 2):
            while bucket[j] == 0:
                j -= 1
            nums[i] = j
            bucket[j] -= 1
        for i in range(0, n, 2):
            while bucket[j] == 0:
                j -= 1
            nums[i] = j
            bucket[j] -= 1
 

The code in other languages (Java, C++, Go, JavaScript) follows similar logic, adapting the syntax and data structures to the respective languages. The core algorithm remains the same. Choose the approach that best fits your needs, considering the trade-off between time complexity and space complexity. If the range of numbers is significantly large, Approach 1 might be preferable, but for the constraints of this problem, Approach 2 is more efficient.