Skip to content

Commit 57160b1

Browse files
committed
Weekly Contest 115
1 parent 02b1a72 commit 57160b1

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Check Completeness of a Binary Tree
2+
class Solution {
3+
int cc;
4+
int c(TreeNode *x) {
5+
return x ? 1 + c(x->left) + c(x->right) : 0;
6+
}
7+
bool f(TreeNode *x, int i) {
8+
return !x || i < cc && f(x->left, 2*i+1) && f(x->right, 2*i+2);
9+
}
10+
public:
11+
bool isCompleteTree(TreeNode *root) {
12+
cc = c(root);
13+
return f(root, 0);
14+
}
15+
};
16+
17+
///
18+
19+
class Solution {
20+
public:
21+
bool isCompleteTree(TreeNode *root) {
22+
vector<TreeNode *> cur{root}, suc;
23+
for(;;) {
24+
bool null = false;
25+
for (auto *x : cur) {
26+
if (!x->left) null = true;
27+
else if (null) return false;
28+
else suc.push_back(x->left);
29+
if (!x->right) null = true;
30+
else if (null) return false;
31+
else suc.push_back(x->right);
32+
}
33+
cur.swap(suc);
34+
suc.clear();
35+
if (null) break;
36+
}
37+
for (auto *x: cur)
38+
if (x->left || x->right)
39+
return false;
40+
return true;
41+
}
42+
};

delete-columns-to-make-sorted-iii.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Delete Columns to Make Sorted III
2+
#define ALL(x) (x).begin(), (x).end()
3+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
4+
#define REP(i, n) FOR(i, 0, n)
5+
6+
class Solution {
7+
public:
8+
int minDeletionSize(vector<string> &A) {
9+
int m = A.size(), n = A[0].size();
10+
vector<int> s(n);
11+
REP(j, n) {
12+
s[j] = 1;
13+
REP(jj, j) {
14+
char ok = 1;
15+
REP(i, m)
16+
ok &= A[i][jj] <= A[i][j];
17+
if (ok)
18+
s[j] = max(s[j], s[jj] + 1);
19+
}
20+
}
21+
return n - *max_element(ALL(s));
22+
}
23+
};

prison-cells-after-n-days.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Prison Cells After N Days
2+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
3+
#define REP(i, n) FOR(i, 0, n)
4+
5+
class Solution {
6+
public:
7+
vector<int> prisonAfterNDays(vector<int> &cells, int N) {
8+
int m = 8, x = 0;
9+
vector<int> memo(1 << m, -1);
10+
REP(i, m)
11+
x |= cells[i] << i;
12+
memo[x] = 0;
13+
FOR(i, 1, N + 1) {
14+
x = ~(x>>1^x<<1) & (1<<m-2)-1<<1;
15+
if (memo[x] >= 0)
16+
i = (N-memo[x])/(i-memo[x])*(i-memo[x])+memo[x];
17+
else
18+
memo[x] = i;
19+
}
20+
vector<int> r(m);
21+
REP(i, m)
22+
r[i] = x >> i & 1;
23+
return r;
24+
}
25+
};

regions-cut-by-slashes.cc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Regions Cut By Slashes
2+
#define ALL(x) (x).begin(), (x).end()
3+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
4+
#define REP(i, n) FOR(i, 0, n)
5+
6+
class Solution {
7+
public:
8+
int regionsBySlashes(vector<string> &g) {
9+
int n = g.size();
10+
vector<int> uf(n*n*4);
11+
iota(ALL(uf), 0);
12+
auto f = [&](int x) {
13+
while (uf[x] != x)
14+
uf[x] = uf[uf[x]], x = uf[x];
15+
return x;
16+
};
17+
auto me = [&](int x, int y) { uf[f(x)] = f(y); };
18+
REP(i, n) REP(j, n) {
19+
int t = (i*n+j)*4;
20+
switch (g[i][j]) {
21+
case '/': me(t+0, t+1); me(t+2, t+3); break;
22+
case '\\': me(t+0, t+3); me(t+1, t+2); break;
23+
default: me(t+0, t+1); me(t+1, t+2); me(t+2, t+3); break;
24+
}
25+
if (i) me(t+0, t-4*n+2);
26+
if (j) me(t+1, t-4+3);
27+
}
28+
int r = 0;
29+
REP(i, n*n*4) r += uf[i] == i;
30+
return r;
31+
}
32+
};

0 commit comments

Comments
 (0)