-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
932.cpp
42 lines (42 loc) · 1.34 KB
/
932.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
34
35
36
37
38
39
40
41
42
__________________________________________________________________________________________________
sample 8 ms submission
class Solution {
public:
vector<int> beautifulArray(int N) {
vector<int> res{1};
while(res.size() < N)
{
vector<int> temp;
for(int i = 0; i < res.size(); ++i)
if(res[i] * 2 - 1 <= N)
temp.push_back(res[i] * 2 - 1);
for(int i = 0; i < res.size(); ++i)
if(res[i] * 2 <= N)
temp.push_back(res[i] * 2);
res = temp;
}
return res;
}
};
__________________________________________________________________________________________________
sample 9036 kb submission
class Solution {
public:
vector<int> beautifulArray(int N) {
vector<int> res{1};
while (res.size() < N) {
vector<int> temp;
for (int i = 0; i < res.size(); i++) {
if (res[i] * 2 - 1 <= N)
temp.push_back(res[i] * 2 - 1);
}
for (int i = 0; i < res.size(); i++) {
if (res[i] * 2 <= N)
temp.push_back(res[i] * 2);
}
swap(temp, res);
}
return res;
}
};
__________________________________________________________________________________________________