Skip to content

Commit ad51266

Browse files
committed
Weekly Contest 98
1 parent f1b6308 commit ad51266

8 files changed

+169
-0
lines changed

boats-to-save-people.cc

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Boats to Save People
2+
class Solution {
3+
public:
4+
int numRescueBoats(vector<int>& a, int limit) {
5+
int i = 0, j = a.size();
6+
sort(a.begin(), a.end());
7+
while (i < j)
8+
if (i < --j && a[i]+a[j] <= limit)
9+
i++;
10+
return a.size()-i;
11+
}
12+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Construct Binary Tree from Preorder and Postorder Traversal
2+
class Solution {
3+
vector<int> pre, post;
4+
TreeNode *f(int i, int j, int n) {
5+
if (!n) return 0;
6+
auto* r = new TreeNode(pre[i]);
7+
if (n > 1) {
8+
int k = find(post.begin()+j, post.begin()+j+n, pre[i+1])-post.begin()-j+1;
9+
r->left = f(i+1, j, k);
10+
r->right = f(i+k+1, j+k, n-k-1);
11+
}
12+
return r;
13+
}
14+
public:
15+
TreeNode* constructFromPrePost(vector<int>& pre, vector<int>& post) {
16+
this->pre = pre;
17+
this->post = post;
18+
return f(0, 0, pre.size());
19+
}
20+
};

decoded-string-at-index.cc

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Decoded String at Index
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+
string decodeAtIndex(string S, int K) {
9+
long n = S.size(), g = 0;
10+
vector<long> a(n), b(n);
11+
REP(i, n)
12+
if (isalpha(S[i]))
13+
a[i] = ++g;
14+
else {
15+
b[i] = g;
16+
a[i] = g *= (S[i]-'0');
17+
}
18+
for (K--; K; ) {
19+
long i = upper_bound(ALL(a), K) - a.begin();
20+
if (!b[i])
21+
string(1, S[i]);
22+
K %= b[i];
23+
}
24+
return string(1, S[0]);
25+
}
26+
};

fair-candy-swap.cc

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Fair Candy Swap
2+
#define ALL(x) (x).begin(), (x).end()
3+
4+
class Solution {
5+
public:
6+
vector<int> fairCandySwap(vector<int>& A, vector<int>& B) {
7+
int d = accumulate(ALL(B), 0) - accumulate(ALL(A), 0) >> 1;
8+
unordered_set<int> BB(ALL(B));
9+
vector<int> r;
10+
for (int a: A)
11+
if (BB.count(a+d)) {
12+
r.push_back(a);
13+
r.push_back(a+d);
14+
break;
15+
}
16+
return r;
17+
}
18+
};

find-and-replace-pattern.cc

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Find and Replace Pattern
2+
class Solution {
3+
string f(string p) {
4+
int x[26], i =0;
5+
fill_n(x, 26, -1);
6+
for (char &c : p) {
7+
if (x[c -= 'a'] < 0)
8+
x[c] = i++;
9+
c = x[c];
10+
}
11+
return p;
12+
}
13+
14+
public:
15+
vector<string> findAndReplacePattern(vector<string>& words, string pattern) {
16+
vector<string> r;
17+
pattern = f(pattern);
18+
for (auto &word : words)
19+
if (f(word) == pattern)
20+
r.push_back(word);
21+
return r;
22+
}
23+
};

projection-area-of-3d-shapes.cc

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Projection Area of 3D Shapes
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 projectionArea(vector<vector<int>> &grid) {
9+
int ans = 0;
10+
for (auto &r: grid)
11+
ans += r.size()-count(ALL(r), 0) + *max_element(ALL(r));
12+
REP(j, grid[0].size()) {
13+
int t = 0;
14+
REP(i, grid.size())
15+
t = max(t, grid[i][j]);
16+
ans += t;
17+
}
18+
return ans;
19+
}
20+
};
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Reachable Nodes In Subdivided Graph
2+
class Solution {
3+
public:
4+
int reachableNodes(vector<vector<int>>& edges, int M, int N) {
5+
vector<vector<pair<int,int>>> g(N);
6+
vector<int> d(N, M+1);
7+
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
8+
vector<char> vis(N);
9+
int ans = 0;
10+
for (auto &r : edges) {
11+
g[r[0]].emplace_back(r[1], r[2] + 1);
12+
g[r[1]].emplace_back(r[0], r[2] + 1);
13+
}
14+
pq.emplace(d[0] = 0, 0);
15+
while (pq.size()) {
16+
int u = pq.top().second;
17+
pq.pop();
18+
if (vis[u]) continue;
19+
vis[u] = 1;
20+
ans++;
21+
for (auto &e : g[u])
22+
if (d[u] + e.second < d[e.first])
23+
pq.emplace(d[e.first] = d[u]+e.second, e.first);
24+
}
25+
for (auto &r : edges) {
26+
int u = r[0], v = r[1], w = r[2];
27+
if (d[u] > d[v])
28+
swap(u, v);
29+
int tu = M-d[u];
30+
if (tu > 0)
31+
ans += min(tu + max(M-d[v], 0), w);
32+
}
33+
return ans;
34+
}
35+
};

sum-of-subsequence-widths.cc

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Sum of Subsequence Widths
2+
class Solution {
3+
public:
4+
int sumSubseqWidths(vector<int>& A) {
5+
const int MOD = 1000000007;
6+
sort(A.begin(), A.end());
7+
long x = 0, c = 0, r = 0;
8+
for (int a: A) {
9+
r += a*c-x;
10+
c = (c*2+1)%MOD;
11+
(x = x*2+a) %= MOD;
12+
}
13+
return (r%MOD+MOD)%MOD;
14+
}
15+
};

0 commit comments

Comments
 (0)