-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeautifulArray.cpp
More file actions
72 lines (65 loc) · 1.73 KB
/
beautifulArray.cpp
File metadata and controls
72 lines (65 loc) · 1.73 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Divide and Conquer
class Solution {
public:
vector<int> beautifulArray(int n) {
if (n == 1) return {1};
else if (n == 2) return {1, 2};
m[1] = {1};
m[2] = {1,2};
return recur(n);
}
vector<int> recur(int n) {
if (m.find(n) != m.end()) return m[n];
vector<int> v(n);
int i = 0;
for (int num: recur((n + 1) / 2))
v[i++] = 2 * num - 1;
for (int num: recur(n / 2))
v[i++] = 2 * num;
m[n] = v;
return v;
}
private:
map<int, vector<int> > m;
};
// DP
class Solution {
public:
vector<int> beautifulArray(int n) {
if (n == 1) return {1};
else if (n == 2) return {1, 2};
vector<vector<int> > dp(n + 1);
dp[1] = {1};
dp[2] = {1, 2};
for (int i = 3; i <= n; i++) {
vector<int> temp(i);
int j = 0;
for (int num: dp[(i + 1) / 2])
temp[j++] = 2 * num - 1;
for (int num: dp[i / 2])
temp[j++] = 2 * num;
dp[i] = temp;
}
return dp[n];
}
};
// sort on bit
class Solution {
public:
vector<int> beautifulArray(int n) {
vector<int> ans(n);
for(int i=0; i<n; i++)
ans[i] = i+1;
auto cmp = [=](int a, int b)
{
int i = 1;
while((a&i)==(b&i))
i = i<<1;
return (a&i) < (b&i);
};
sort(ans.begin(), ans.end(), cmp);
return ans;
}
};
// https://leetcode-cn.com/problems/beautiful-array/solution/zhe-shi-shuang-100de-da-an-c-by-jameswan-jo0r/
// https://leetcode-cn.com/problems/beautiful-array/solution/kan-liao-yi-xia-da-shen-de-dai-ma-shuo-yi-xia-zi-j/