Skip to content

Commit 05f82d7

Browse files
committed
0324 solved.
1 parent 70a4718 commit 05f82d7

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
project(cpp_0324)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(cpp_0324 main.cpp)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/// Source : https://leetcode.com/problems/wiggle-sort-ii/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-06-27
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <algorithm>
8+
#include <cassert>
9+
10+
using namespace std;
11+
12+
13+
/// Ad-Hoc
14+
/// Time Complexity: O(nlogn)
15+
/// Space Complexity: O(n)
16+
class Solution {
17+
public:
18+
void wiggleSort(vector<int>& nums) {
19+
20+
int n = nums.size();
21+
22+
sort(nums.begin(), nums.end());
23+
24+
vector<int> res(n);
25+
int index = 0;
26+
for(int i = 0; i < n; i += 2) res[i] = nums[index ++];
27+
for(int i = 1; i < n; i += 2) res[i] = nums[index ++];
28+
29+
if(ok(n, res)){
30+
nums = res;
31+
return;
32+
}
33+
34+
assert(n % 2 == 0);
35+
36+
index = 0;
37+
for(int i = 1; i < n; i += 2) res[i] = nums[index ++];
38+
for(int i = 0; i < n; i += 2) res[i] = nums[index ++];
39+
reverse(res.begin(), res.end());
40+
assert(ok(n, res));
41+
nums = res;
42+
}
43+
44+
private:
45+
bool ok(int n, const vector<int>& a){
46+
47+
for(int i = 1; i + 1 < n; i ++){
48+
if(i % 2 == 1){
49+
if(!(a[i - 1] < a[i] && a[i] > a[i + 1])) return false;
50+
}
51+
else{
52+
if(!(a[i - 1] > a[i] && a[i] < a[i + 1])) return false;
53+
}
54+
}
55+
return true;
56+
}
57+
};
58+
59+
60+
void print_vec(const vector<int>& v){
61+
for(int e: v) cout << e << ' '; cout << '\n';
62+
}
63+
64+
int main() {
65+
66+
vector<int> nums1 = {4, 5, 5, 6};
67+
Solution().wiggleSort(nums1);
68+
print_vec(nums1);
69+
70+
return 0;
71+
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
359359
| 321 | [Create Maximum Number](https://leetcode.com/problems/create-maximum-number/) | [] | [C++](0001-0500/0321-Create-Maximum-Number/cpp-0321/) | | |
360360
| 322 | [Coin Change](https://leetcode.com/problems/coin-change/description/) | [solution](https://leetcode.com/problems/coin-change/solution/) | [C++](0001-0500/0322-Coin-Change/cpp-0322/) | | |
361361
| 323 | [Number of Connected Components in an Undirected Graph](https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/) | [solution](https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/solution/) | [C++](0001-0500/0323-Number-of-Connected-Components-in-an-Undirected-Graph/cpp-0323/) | | |
362-
| | | | | | |
362+
| 324 | [Wiggle Sort II](https://leetcode.com/problems/wiggle-sort-ii/) | [] | [C++](0001-0500/0324-Wiggle-Sort-II/cpp-0324/) | | |
363363
| 325 | [Maximum Size Subarray Sum Equals k](https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/) | [solution](https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/solution/) | [C++](0001-0500/0325-Maximum-Size-Subarray-Sum-Equals-k/cpp-0325/) | | |
364364
| 326 | [Power of Three](https://leetcode.com/problems/power-of-three/) | [solution](https://leetcode.com/problems/power-of-three/solution/) | [C++](0001-0500/0326-Power-of-Three/cpp-0326/) | | |
365365
| | | | | | |

0 commit comments

Comments
 (0)