forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wiggle-sort-ii.cpp
33 lines (30 loc) · 911 Bytes
/
wiggle-sort-ii.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Time: O(n)
// Space: O(n)
class Solution {
public:
void wiggleSort(vector<int>& nums) {
const int n = nums.size();
int mid = (n - 1) / 2;
nth_element(nums.begin(), nums.begin() + mid, nums.end());
dutchFlagSort(nums, nums[mid]);
vector<int> res(n);
for (int i = 0, smallEnd = mid; i < n; i += 2, --smallEnd) {
res[i] = nums[smallEnd];
}
for (int i = 1, largeEnd = n - 1; i < n; i += 2, --largeEnd) {
res[i] = nums[largeEnd];
}
nums = res;
}
void dutchFlagSort(vector<int>& nums, int val) {
for (int i = 0, j = 0, n = nums.size() - 1; j <= n;) {
if (nums[j] < val) {
swap(nums[i++], nums[j++]);
} else if (nums[j] > val) {
swap(nums[j], nums[n--]);
} else {
++j;
}
}
}
};