Leetcode 324: Wiggle Sort II
You are given an array of integers. Your task is to reorder the array such that the elements alternate between smaller and larger numbers. The sequence should follow the pattern nums[0] < nums[1] > nums[2] < nums[3] > nums[4]….
Problem
Approach
Steps
Complexity
Input: You are given an array of integers.
Example: nums = [3, 5, 2, 1, 6, 4]
Constraints:
• 1 <= nums.length <= 5 * 10^4
• 0 <= nums[i] <= 5000
Output: Return the reordered array where the elements alternate between smaller and larger numbers, following the pattern nums[0] < nums[1] > nums[2] < nums[3] > nums[4]....
Example: [3, 5, 2, 6, 1, 4]
Constraints:
• The array should be reordered in place with the alternating pattern.
Goal: To reorder the given array such that the elements alternate between smaller and larger numbers.
Steps:
• Sort the array first.
• Rearrange the elements in the array using a two-pointer technique to achieve the alternating pattern.
• Handle the case when the array length is odd or even while ensuring the alternating condition holds.
Goal: The solution should handle both small and large input sizes efficiently.
Steps:
• The input array length can go up to 50,000 elements.
• Each element of the array is between 0 and 5000.
Assumptions:
• The input array is always guaranteed to have a valid answer.
• Input: nums = [3, 5, 2, 1, 6, 4]
• Explanation: The reordered array follows the alternating pattern: 3 < 5 > 2 < 6 > 1 < 4.
• Input: nums = [2, 1, 3, 2, 1, 4]
• Explanation: The reordered array follows the alternating pattern: 1 < 3 > 1 < 4 > 2 < 2.
Approach: We can solve this problem efficiently using sorting followed by a two-pointer technique to reorder the array.
Observations:
• The alternating pattern can be easily achieved by first sorting the array.
• We need to rearrange the array in a way that satisfies the alternating condition.
• Sorting the array ensures that we can easily access the smallest and largest elements to form the alternating pattern.
Steps:
• Sort the input array.
• Use two pointers: one starting from the smallest element and one from the largest, and alternate between the two to build the reordered array.
Empty Inputs:
• The array is guaranteed to have at least one element, so an empty array case is not possible.
Large Inputs:
• The solution should be efficient enough to handle large inputs with up to 50,000 elements.
Special Values:
• If the array contains repeated elements, the alternating pattern should still hold.
Constraints:
• Ensure the solution runs in O(n) time after sorting the array.
void wiggleSort(vector<int>& nums) {
int n = nums.size();
auto midptr = nums.begin() + n / 2;
nth_element(nums.begin(), midptr, nums.end());
int mid = *midptr;
#define A(i) nums[(1 + 2 * i) % (n|1)]
int i = 0, j = 0, k = n - 1;
while(j <= k) {
if(A(j) < mid)
swap(A(j), A(k--));
else if(A(j) > mid)
swap(A(i++), A(j++));
else j++;
}
}
1 : Function Declaration
void wiggleSort(vector<int>& nums) {
This is the declaration of the `wiggleSort` function, which takes a reference to a vector of integers (`nums`) and rearranges the elements in a wiggle pattern.
2 : Variable Initialization
int n = nums.size();
Here, the variable `n` is initialized to store the size of the `nums` vector.
3 : Median Calculation
auto midptr = nums.begin() + n / 2;
The pointer `midptr` is set to the middle element of the `nums` vector, which will be used to find the median.
4 : Median Calculation
nth_element(nums.begin(), midptr, nums.end());
The `nth_element` function rearranges the elements in the vector such that the element at the middle pointer (`midptr`) is the median of the array, with all smaller elements on the left and larger ones on the right.
5 : Variable Initialization
int mid = *midptr;
The median value (`mid`) is assigned by dereferencing the `midptr` pointer.
6 : Macro Definition
#define A(i) nums[(1 + 2 * i) % (n|1)]
This macro defines the function `A(i)` to access the elements in a rearranged order, ensuring that the elements are accessed in a zigzag manner, alternating between odd and even indices.
7 : Variable Initialization
int i = 0, j = 0, k = n - 1;
Here, three variables (`i`, `j`, and `k`) are initialized to represent three pointers for the rearrangement process: `i` for the leftmost part, `j` for the current element, and `k` for the rightmost part.
8 : Loop Initialization
while(j <= k) {
This while loop continues until the `j` pointer exceeds the `k` pointer, indicating that all elements have been rearranged.
9 : Conditional Logic
if(A(j) < mid)
In this condition, if the current element pointed to by `j` is less than the median (`mid`), it means the element should be moved to the left part of the array.
10 : Swap Operation
swap(A(j), A(k--));
Here, the element at `A(j)` is swapped with the element at `A(k)`, and the `k` pointer is decremented to move the right pointer inward.
11 : Conditional Logic
else if(A(j) > mid)
If the current element at `A(j)` is greater than the median (`mid`), it should be placed in the right part of the array.
12 : Swap Operation
swap(A(i++), A(j++));
In this case, the element at `A(i)` is swapped with the element at `A(j)`, and both pointers are incremented to move inward.
13 : Conditional Logic
else j++;
If the current element at `A(j)` is equal to the median (`mid`), we simply increment the `j` pointer to continue checking the next element.
Best Case: O(n log n)
Average Case: O(n log n)
Worst Case: O(n log n)
Description: The time complexity is dominated by the sorting step, which takes O(n log n) time.
Best Case: O(1)
Worst Case: O(n)
Description: The space complexity is O(n) due to the storage used for the sorted array. The in-place rearrangement takes O(1) additional space.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus