array and hashmap November 7, 2022

Wiggle sort II

Time O(nlogn) Space O(1) Open original problem

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)