↓Skip to main content
  1. LeetCode/

LeetCode 324: Wiggle Sort II

·2 mins· ·
LeetCode Medium Array Divide-and-Conquer Greedy Sorting
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2022-11-17

  • Submissions: Wrong Answer → Runtime Error → Accepted → Wrong Answer

Approach
#

nums[0] < nums[1] > nums[2] < ... puts small values at even indices and large ones at odd indices. After sorting, the smaller ceil(n / 2) go to even indices and the rest to odd ones, but both halves must be reversed: in ascending order the end of the small half and the start of the large half may be equal (a repeated median) and they would land next to each other. Reversed, equal medians end up at opposite ends. The O(n) time, O(1) space follow-up needs quickselect for the median plus a 3-way partition with virtual indices.

My Attempts
#

Attempt 1 interleaved in ascending order, putting the repeated median side by side: [2, 4, 1, 2] sorts to [1, 2, 2, 4] and interleaving gives [1, 2, 2, 4], with two adjacent 2s. Reversing both halves fixed it after one slice-length mistake (ValueError).

Solution
#

class Solution:
    def wiggleSort(self, nums: List[int]) -> None:
        s = sorted(nums)
        half = (len(nums) + 1) // 2
        nums[::2] = s[:half][::-1]
        nums[1::2] = s[half:][::-1]

Complexity
#

Time O(n log n), space O(n).

Mistakes to Avoid
#

  • Reverse both halves, or repeated medians become adjacent.
  • The small half holds ceil(n / 2) values and goes to even indices.

Interview Explanation
#

After sorting I place the smaller half at even indices and the larger half at odd indices, both reversed so repeated medians end up far apart. Time O(n log n); the O(n) follow-up uses quickselect and a three-way partition.

Related

LeetCode 435: Non-overlapping Intervals
·2 mins
LeetCode Medium Array Dynamic-Programming Greedy Sorting
LeetCode 435 note: minimum removals = total minus the maximum kept; sort by right end and greedily keep the one ending earliest
LeetCode 215: Kth Largest Element in an Array
·2 mins
LeetCode Medium Array Divide-and-Conquer Sorting Heap-Priority-Queue
LeetCode 215 note: keep a min-heap of size k whose top is the k-th largest, or use quickselect for O(n) on average
LeetCode 347: Top K Frequent Elements
·1 min
LeetCode Medium Array Hash-Table Divide-and-Conquer Sorting
LeetCode 347 note: count first, then take the k most frequent with a size-k heap or a bucket sort indexed by count
LeetCode 148: Sort List
·2 mins
LeetCode Medium Linked-List Two-Pointers Divide-and-Conquer Sorting
LeetCode 148 note: split in half with fast and slow pointers, sort each half recursively and merge; O(n log n) with no extra array
LeetCode 334: Increasing Triplet Subsequence
·2 mins
LeetCode Medium Array Greedy Longest-Increasing-Subsequence
LeetCode 334 note: keep the smallest first and the smallest second that has something smaller before it; any number above second completes a triplet
LeetCode 229: Majority Element II
·2 mins
LeetCode Medium Array Hash-Table Sorting Counting
LeetCode 229 note: at most two values can exceed n / 3; Boyer-Moore keeps two candidates and a second pass verifies their counts