Wiggle sort II

November 7, 2022

array-and-hashmap

Problem URL: Wiggle sort II

We will sort the array and then split it into two halves. We will iterate over the first half and the second half and insert the elements into the result array.

class Solution:
    def wiggleSort(self, nums: List[int]) -> None:
        nums.sort()
        half = len(nums[::2])-1
        nums[::2], nums[1::2] = nums[half::-1], nums[:half:-1]

Time complexity: O(nlogn)
Space complexity: O(1)