<p>Given an integer array <code>nums</code>, reorder it such that <code>nums[0] &lt; nums[1] &gt; nums[2] &lt; nums[3]...</code>.</p>

<p>You may assume the input array always has a valid answer.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,5,1,1,6,4]
<strong>Output:</strong> [1,6,1,5,1,4]
<strong>Explanation:</strong> [1,4,1,5,1,6] is also accepted.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,3,2,2,3,1]
<strong>Output:</strong> [2,3,1,3,1,2]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>4</sup></code></li>
	<li><code>0 &lt;= nums[i] &lt;= 5000</code></li>
	<li>It is guaranteed that there will be an answer for the given input <code>nums</code>.</li>
</ul>

<p>&nbsp;</p>
<strong>Follow Up:</strong> Can you do it in <code>O(n)</code> time and/or <strong>in-place</strong> with <code>O(1)</code> extra space?
class Solution:
    def wiggleSort(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        # Find the median using QuickSelect
        def find_median(nums):
            def partition(nums, left, right):
                pivot_index = (left + right) // 2
                pivot = nums[pivot_index]
                nums[pivot_index], nums[right] = nums[right], nums[pivot_index]
                i = left
                for j in range(left, right):
                    if nums[j] <= pivot:
                        nums[i], nums[j] = nums[j], nums[i]
                        i += 1
                nums[i], nums[right] = nums[right], nums[i]
                return i

            def quickselect(nums, k):
                left, right = 0, len(nums) - 1
                while left <= right:
                    pivot_index = partition(nums, left, right)
                    if pivot_index == k:
                        return nums[k]
                    elif pivot_index < k:
                        left = pivot_index + 1
                    else:
                        right = pivot_index - 1

            return quickselect(nums, len(nums) // 2)

        # Three-way partition around the median
        def three_way_partition(nums, median):
            def virtual_index(i, n):
                return (1 + 2 * i) % (n | 1)

            n = len(nums)
            median_index = nums.index(median)
            nums[median_index], nums[-1] = nums[-1], nums[median_index]
            i, j, k = 0, 0, n - 1
            while j <= k:
                if nums[virtual_index(j, n)] > median:
                    nums[virtual_index(i, n)], nums[virtual_index(j, n)] = nums[virtual_index(j, n)], nums[virtual_index(i, n)]
                    i += 1
                    j += 1
                elif nums[virtual_index(j, n)] < median:
                    nums[virtual_index(j, n)], nums[virtual_index(k, n)] = nums[virtual_index(k, n)], nums[virtual_index(j, n)]
                    k -= 1
                else:
                    j += 1

        # Main function
        median = find_median(nums)
        three_way_partition(nums, median)

# Example usage:
solution = Solution()
nums = [1, 5, 1, 1, 6, 4]
solution.wiggleSort(nums)
print(nums)  # Output: [1, 6, 1, 5, 1, 4] or [1, 4, 1, 5, 1, 6]
19200