324. Wiggle Sort II

problem

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
void wiggleSort(vector<int>& nums) {
sort(nums.begin(), nums.end());
int n = nums.size();
int l = (n-1)/2, r = n-1;
vector<int> ret(n,0);
for(int i=0;i<n;++i){
if(i%2==0) ret[i] = nums[l--];
else ret[i] = nums[r--];
}
nums = ret;
}
};

analysis

  • time complexity O(nlogn)
  • space complexity O(n)