Skip to content

Commit 23d1b06

Browse files
committedApr 21, 2019
Weekly Contest 133
1 parent ca9ef48 commit 23d1b06

4 files changed

+109
-0
lines changed
 

‎matrix-cells-in-distance-order.cc

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Matrix Cells in Distance Order
2+
class Solution {
3+
public:
4+
vector<vector<int>> allCellsDistOrder(int R, int C, int r0, int c0) {
5+
unsigned r = R, c = C;
6+
int x, y;
7+
vector<vector<int>> a;
8+
for (int i = 0; i < R+C-1; i++)
9+
for (int j = 0; j <= i; j++) {
10+
x = r0+j;
11+
if (x < r) {
12+
y = c0+i-j; if (y < c) a.push_back({x, y});
13+
y = c0-i+j; if (j < i && y < c) a.push_back({x, y});
14+
}
15+
x = r0-j;
16+
if (j && x < r) {
17+
y = c0+i-j; if (y < c) a.push_back({x, y});
18+
y = c0-i+j; if (j < i && y < c) a.push_back({x, y});
19+
}
20+
}
21+
return a;
22+
}
23+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Maximum Sum of Two Non-Overlapping Subarrays
2+
class Solution {
3+
public:
4+
int maxSumTwoNoOverlap(vector<int>& A, int L, int M) {
5+
int n = A.size(), r = 0;
6+
vector<int> s(n+1);
7+
for (int i = 0; i < n; i++)
8+
s[i+1] = s[i]+A[i];
9+
for (int _ = 2; _--; swap(L, M)) {
10+
int m = 0;
11+
for (int i = L+M; i <= n; i++) {
12+
m = max(m, s[i-M]-s[i-M-L]);
13+
r = max(r, s[i]-s[i-M]+m);
14+
}
15+
}
16+
return r;
17+
}
18+
};

‎stream-of-characters.cc

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Stream of Characters
2+
class StreamChecker {
3+
struct Node { int c[26], pi, end; } *pool, *allo, *x;
4+
int sz;
5+
public:
6+
StreamChecker(vector<string> &words) {
7+
sz = 1;
8+
pool = (Node*)malloc(sizeof(Node));
9+
allo = pool;
10+
memset(allo++, 0, sizeof(Node));
11+
for (auto &w: words) {
12+
Node *x = pool;
13+
for (char c: w) {
14+
if (!x->c[c-'a']) {
15+
if (allo-pool >= sz) {
16+
sz *= 2;
17+
auto pool2 = (Node*)realloc(pool, sizeof(Node)*sz);
18+
x = x-pool + pool2;
19+
allo = allo-pool + pool2;
20+
pool = pool2;
21+
}
22+
memset(allo, 0, sizeof(Node));
23+
x->c[c-'a'] = allo++ - pool;
24+
}
25+
x = pool+x->c[c-'a'];
26+
}
27+
x->end = 1;
28+
}
29+
auto q = new int[allo-pool];
30+
int qs = 0;
31+
for (int i = 0; i < 26; i++)
32+
if (pool->c[i])
33+
q[qs++] = pool->c[i];
34+
for (int i = 0; i < qs; i++) {
35+
Node *x = pool+q[i];
36+
x->end |= pool[x->pi].end;
37+
for (int c = 0; c < 26; c++)
38+
if (!x->c[c])
39+
x->c[c] = pool[x->pi].c[c];
40+
else {
41+
pool[x->c[c]].pi = pool[x->pi].c[c];
42+
q[qs++] = x->c[c];
43+
}
44+
}
45+
x = pool;
46+
}
47+
48+
~StreamChecker() { free(pool); }
49+
50+
bool query(char letter) {
51+
x = pool+x->c[letter-'a'];
52+
return x->end;
53+
}
54+
};

‎two-city-scheduling.cc

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Two City Scheduling
2+
class Solution {
3+
public:
4+
int twoCitySchedCost(vector<vector<int>>& costs) {
5+
int n = costs.size()/2;
6+
nth_element(costs.begin(), costs.begin()+n, costs.end(), [](auto &x, auto &y) {
7+
return x[0]-x[1] < y[0]-y[1];
8+
});
9+
int r = 0;
10+
for (int i = 0; i < n; i++)
11+
r += costs[i][0]+costs[i+n][1];
12+
return r;
13+
}
14+
};

0 commit comments

Comments
 (0)