Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 * 1040 <= nums[i] <= 5000nums.O(n) time and/or in-place with O(1) extra space?Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[1,5,1,1,6,4]
[1,3,2,2,3,1]
sort-colors)kth-largest-element-in-an-array)wiggle-sort)array-with-elements-not-equal-to-average-of-neighbors)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #324: Wiggle Sort II
class Solution {
public void wiggleSort(int[] nums) {
int[] arr = nums.clone();
Arrays.sort(arr);
int n = nums.length;
int i = (n - 1) >> 1, j = n - 1;
for (int k = 0; k < n; ++k) {
if (k % 2 == 0) {
nums[k] = arr[i--];
} else {
nums[k] = arr[j--];
}
}
}
}
// Accepted solution for LeetCode #324: Wiggle Sort II
func wiggleSort(nums []int) {
n := len(nums)
arr := make([]int, n)
copy(arr, nums)
sort.Ints(arr)
i, j := (n-1)>>1, n-1
for k := 0; k < n; k++ {
if k%2 == 0 {
nums[k] = arr[i]
i--
} else {
nums[k] = arr[j]
j--
}
}
}
# Accepted solution for LeetCode #324: Wiggle Sort II
class Solution:
def wiggleSort(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
arr = sorted(nums)
n = len(arr)
i, j = (n - 1) >> 1, n - 1
for k in range(n):
if k % 2 == 0:
nums[k] = arr[i]
i -= 1
else:
nums[k] = arr[j]
j -= 1
// Accepted solution for LeetCode #324: Wiggle Sort II
struct Solution;
impl Solution {
fn wiggle_sort(nums: &mut Vec<i32>) {
let n = nums.len();
let mut sorted = nums.to_vec();
sorted.sort_unstable();
let k = if n % 2 == 0 { n / 2 } else { n / 2 + 1 };
for i in 0..k {
nums[i * 2] = sorted[k - 1 - i];
}
for i in 0..(n - k) {
nums[i * 2 + 1] = sorted[n - 1 - i];
}
}
}
#[test]
fn test() {
let mut nums = vec![1, 5, 1, 1, 6, 4];
let res = vec![1, 6, 1, 5, 1, 4];
Solution::wiggle_sort(&mut nums);
assert_eq!(nums, res);
let mut nums = vec![1, 1, 2, 1, 2, 2, 1];
let res = vec![1, 2, 1, 2, 1, 2, 1];
Solution::wiggle_sort(&mut nums);
assert_eq!(nums, res);
let mut nums = vec![4, 5, 5, 6];
let res = vec![5, 6, 4, 5];
Solution::wiggle_sort(&mut nums);
assert_eq!(nums, res);
}
// Accepted solution for LeetCode #324: Wiggle Sort II
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #324: Wiggle Sort II
// class Solution {
// public void wiggleSort(int[] nums) {
// int[] arr = nums.clone();
// Arrays.sort(arr);
// int n = nums.length;
// int i = (n - 1) >> 1, j = n - 1;
// for (int k = 0; k < n; ++k) {
// if (k % 2 == 0) {
// nums[k] = arr[i--];
// } else {
// nums[k] = arr[j--];
// }
// }
// }
// }
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.