-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy path526. Beautiful Arrangement.cpp
147 lines (130 loc) · 4.36 KB
/
526. Beautiful Arrangement.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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//backtrack
//Runtime: 536 ms, faster than 19.94% of C++ online submissions for Beautiful Arrangement.
//Memory Usage: 6 MB, less than 71.95% of C++ online submissions for Beautiful Arrangement.
//time: O(#permutations), space: O(N)
class Solution {
public:
int N;
void backtrack(int& count, vector<int>& perm, vector<bool>& used){
if(perm.size() == N){
count++;
}else{
for(int i = 0; i < N; i++){
//i+1: the value to be inserted
//perm.size()+1: the value's index in perm(1-based)
if(!used[i] && ((i+1) % (perm.size()+1) == 0 || (perm.size()+1) % (i+1) == 0)){
used[i] = true;
perm.push_back(i);
backtrack(count, perm, used);
used[i] = false;
perm.pop_back();
}
}
}
}
int countArrangement(int N) {
this->N = N;
int count = 0;
vector<int> perm;
vector<bool> used(N, false);
backtrack(count, perm, used);
return count;
}
};
//optimized backtracking
//Runtime: 360 ms, faster than 37.06% of C++ online submissions for Beautiful Arrangement.
//Memory Usage: 6.1 MB, less than 61.13% of C++ online submissions for Beautiful Arrangement.
class Solution {
public:
int N;
int backtrack(int cur, bitset<15>& used){
if(cur > N){
return 1;
}else{
int count = 0;
for(int i = 0; i < N; i++){
//i+1: the value to be inserted
//cur: the value's index in perm(1-based)
if(!used[i] && ((i+1) % cur == 0 || cur % (i+1) == 0)){
used[i] = 1;
count += backtrack(cur+1, used);
used[i] = 0;
}
}
return count;
}
}
int countArrangement(int N) {
this->N = N;
bitset<15> used;
return backtrack(1, used);
}
};
//recursion with memorization
//Runtime: 88 ms, faster than 72.42% of C++ online submissions for Beautiful Arrangement.
//Memory Usage: 9.3 MB, less than 20.58% of C++ online submissions for Beautiful Arrangement.
class Solution {
public:
int N;
map<pair<int, int>, int> memo;
int backtrack(int cur, bitset<15>& used){
pair<int, int> key = make_pair(cur, used.to_ulong());
if(cur > N){
return 1;
}else if(memo.find(key) != memo.end()){
return memo[key];
}else{
for(int i = 0; i < N; i++){
//i+1: the value to be inserted
//cur: the value's index in perm(1-based)
if(!used[i] && ((i+1) % cur == 0 || cur % (i+1) == 0)){
used[i] = 1;
memo[key] += backtrack(cur+1, used);
used[i] = 0;
}
}
return memo[key];
}
}
int countArrangement(int N) {
this->N = N;
bitset<15> used;
return backtrack(1, used);
}
};
//top-down DP/recursion with memorization
//Runtime: 16 ms, faster than 88.26% of C++ online submissions for Beautiful Arrangement.
//Memory Usage: 11.6 MB, less than 19.82% of C++ online submissions for Beautiful Arrangement.
class Solution {
public:
int N;
vector<vector<int>> dp;
int backtrack(int cur, bitset<15>& used){
int kused = used.to_ulong();
if(cur > N){
return 1;
}else if(dp[cur][kused] != -1){
return dp[cur][kused];
}else{
dp[cur][kused] = 0;
for(int i = 0; i < N; i++){
//i+1: the value to be inserted
//cur: the value's index in perm(1-based)
if(!used[i] && ((i+1) % cur == 0 || cur % (i+1) == 0)){
used[i] = 1;
dp[cur][kused] += backtrack(cur+1, used);
used[i] = 0;
}
}
// cout << "(" << cur << ", " << kused << "): " << dp[cur][kused] << endl;
return dp[cur][kused];
}
}
int countArrangement(int N) {
this->N = N;
bitset<15> used;
dp = vector<vector<int>>(N+1, vector<int>(1<<N, -1));
backtrack(1, used);
return dp[1][0];
}
};