1332. Strict Zigzag Arrangement
A florist wants to line up flowers whose heights are given in nums so that the heights alternate strictly: r[0] < r[1] > r[2] < r[3] > ..., where each even position is lower than its neighbours and each odd position is higher than its neighbours. Return any permutation r of nums that follows this pattern, or an empty array if no such ordering exists. Every valid answer is accepted, so the order you choose among valid arrangements does not matter. A single flower always forms a valid line. Equal heights can never touch, so a height that appears too often makes the task impossible. Aim for O(n log n) time and O(n) extra space.
Example 1
- Input:
- nums = [6,2,9,2,7,4]
- Output:
- [4,9,2,7,2,6]
- Explanation:
One valid line is 4 < 9 > 2 < 7 > 2 < 6, which uses every height exactly once.
Example 2
- Input:
- nums = [2,2,2]
- Output:
- []
- Explanation:
All heights are equal, and equal neighbours can never alternate strictly, so the result is empty.
Example 3
- Input:
- nums = [4,3,2,1]
- Output:
- [2,4,1,3]
- Explanation:
One valid line is 2 < 4 > 1 < 3, which uses every height exactly once.
Constraints
1 ≤ n ≤ 1000
0 ≤ nums[i] ≤ 109
How this problem is judged
- Answers
- Any valid answer is accepted. A checker tests yours against the problem's rules.
Expected complexity
- Time
- O(n log n)
- Space
- O(n)