Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Wiggle Sort II

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "324" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

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?

Explanation

Here's a breakdown of the approach, followed by the Python code:

  • Find the Median: The core idea is to find the median of the array. Elements smaller than the median will occupy the even indices, and elements larger than the median will occupy the odd indices.
  • Partition Around Median: Partition the array around the median. This puts smaller elements to the left and larger elements to the right, with the median in the middle (though not necessarily sorted).
  • Wiggle Sort: Carefully place the elements from the partitioned array into the nums array in the desired wiggle pattern. This is done by iterating through the 'smaller' and 'larger' sections of the partitioned array and placing elements into the appropriate indices of the original array.

  • Runtime Complexity: O(n)

  • Storage Complexity: O(1)

Code

    class Solution:
    def wiggleSort(self, nums: list[int]) -> None:
        """
        Reorders the nums array such that nums[0] < nums[1] > nums[2] < nums[3]....
        """
        n = len(nums)
        # Find the median using the nth_element algorithm (Hoare's selection algorithm)

        def find_median(nums):
            k = len(nums) // 2
            l, r = 0, len(nums) - 1

            while True:
                pivot = nums[r]
                i = l
                for j in range(l, r):
                    if nums[j] <= pivot:
                        nums[i], nums[j] = nums[j], nums[i]
                        i += 1
                nums[i], nums[r] = nums[r], nums[i]

                if k == i:
                    return nums[i]
                elif k < i:
                    r = i - 1
                else:
                    l = i + 1

        median = find_median(nums.copy())  # Use a copy to avoid modifying the original during median finding

        # Indexing map
        def index(i):
            return (2 * i + 1) % (n | 1)

        # 3-way partition around the median in virtual indexing
        l, r, i = 0, n - 1, 0
        while i <= r:
            if nums[index(i)] > median:
                nums[index(i)], nums[index(l)] = nums[index(l)], nums[index(i)]
                l += 1
                i += 1
            elif nums[index(i)] < median:
                nums[index(i)], nums[index(r)] = nums[index(r)], nums[index(i)]
                r -= 1
            else:
                i += 1

More from this blog

C

Chatmagic blog

2894 posts

Solving Leetcode Interviews in Seconds with AI: Wiggle Sort II