Skip to content

Commit d05b888

Browse files
committed
Weekly Contest 163
1 parent 87777f7 commit d05b888

4 files changed

+113
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Find Elements in a Contaminated Binary Tree
2+
class FindElements {
3+
unordered_set<int> s;
4+
void f(TreeNode *x, int v) {
5+
if (!x) return;
6+
x->val = v;
7+
s.insert(v);
8+
f(x->left, 2*v+1);
9+
f(x->right, 2*v+2);
10+
}
11+
public:
12+
FindElements(TreeNode* root) {
13+
f(root, 0);
14+
}
15+
bool find(int target) {
16+
return s.count(target);
17+
}
18+
};

greatest-sum-divisible-by-three.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Greatest Sum Divisible by Three
2+
class Solution {
3+
public:
4+
int maxSumDivThree(vector<int>& nums) {
5+
int tot = 0, ans = 0, a[2] = {10001, 10001}, b[2] = {10001, 10001};
6+
for (int x: nums) {
7+
int k = x%3;
8+
if (k--) {
9+
b[k] = min(b[k], x);
10+
if (b[k] < a[k]) swap(a[k], b[k]);
11+
}
12+
tot += x;
13+
}
14+
int k = tot%3;
15+
if (!k--)
16+
return tot;
17+
if (a[k] < 10001)
18+
ans = tot-a[k];
19+
if (b[k^1] < 10001)
20+
ans = max(ans, tot-a[k^1]-b[k^1]);
21+
return ans;
22+
}
23+
};
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Minimum Moves to Move a Box to Their Target Location
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+
int vis[20][20][20];
6+
7+
class Solution {
8+
public:
9+
int minPushBox(vector<vector<char>>& grid) {
10+
using pii = pair<int, int>;
11+
using state = pair<pii, pii>;
12+
int n = grid.size(), m = grid[0].size();
13+
pii src, box, tar;
14+
const int dirs[][2] = {{0,-1},{-1,0},{0,1},{1,0}};
15+
REP(i, n) REP(j, m)
16+
switch (grid[i][j]) {
17+
case 'S': src = {i, j}; break;
18+
case 'B': box = {i, j}; break;
19+
case 'T': tar = {i, j}; break;
20+
}
21+
memset(vis, 0, sizeof vis);
22+
deque<state> q;
23+
q.push_back({src, box});
24+
vis[src.first][src.second][box.first] = 1 << box.second;
25+
int d = 0, e = 1;
26+
while (q.size()) {
27+
state c = q.front();
28+
q.pop_front();
29+
if (e) e--;
30+
else e = q.size(), d++;
31+
for (auto dir: dirs) {
32+
unsigned x = c.first.first+dir[0], y = c.first.second+dir[1];
33+
if (x >= n || y >= m || grid[x][y] == '#') continue;
34+
state s;
35+
int cost = 0;
36+
if (pii(x, y) == c.second) {
37+
unsigned xx = x+dir[0], yy = y+dir[1];
38+
if (xx >= n || yy >= m || grid[xx][yy] == '#') continue;
39+
if (pii(xx, yy) == tar) return d+1;
40+
s = {{x, y}, {xx, yy}};
41+
cost = 1;
42+
} else
43+
s = {{x, y}, c.second};
44+
int &v = vis[x][y][s.second.first];
45+
if (!(v & 1 << s.second.second)) {
46+
v |= 1 << s.second.second;
47+
if (cost) q.push_back(s);
48+
else e++, q.push_front(s);
49+
}
50+
}
51+
}
52+
return -1;
53+
}
54+
};

shift-2d-grid.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Shift 2D Grid
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<vector<int>> shiftGrid(vector<vector<int>>& grid, int k) {
8+
int n = grid.size(), m = grid[0].size(), g = 0;
9+
vector<int> a(n*m);
10+
REP(i, n) REP(j, m)
11+
a[g++] = grid[i][j];
12+
rotate(a.begin(), a.end()-k%(n*m), a.end());
13+
g = 0;
14+
REP(i, n) REP(j, m)
15+
grid[i][j] = a[g++];
16+
return grid;
17+
}
18+
};

0 commit comments

Comments
 (0)