Given an integer array nums, transform it into a wiggle array where elements alternate between peaks and valleys in a strict pattern: nums[0] < nums[1] > nums[2] < nums[3] > nums[4] < nums[5]...
Think of it like creating a zigzag mountain range where each peak is higher than its neighboring valleys, and each valley is lower than its neighboring peaks. The pattern must be strictly alternating - no equal adjacent elements allowed!
Example: [1,5,1,1,6,4] โ [1,6,1,5,1,4] creates the pattern: small < large > small < large > small < large
You're guaranteed that a valid wiggle arrangement always exists for the input array.
Input & Output
Time & Space Complexity
O(n!) to generate all permutations, O(n) to check each permutation
Space for storing one permutation at a time during generation
Constraints
- 1 โค nums.length โค 5 ร 104
- 0 โค nums[i] โค 5000
- You may assume the input array always has a valid answer
- The array is modified in-place with O(1) extra memory for the optimal solution