Skip to content

Commit 3eb127e

Browse files
committed
Weekly Contest 110
1 parent 67b3ee0 commit 3eb127e

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

distinct-subsequences-ii.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Distinct Subsequences II
2+
class Solution {
3+
public:
4+
int distinctSubseqII(string a) {
5+
int mod = 1000000007, n = a.size(), s = 1;
6+
vector<int> bs(26, -1);
7+
for (int i = 0; i < n; i++) {
8+
int &b = bs[a[i]-'a'];
9+
b = exchange(s, (s*2L-(~b?b:0)+mod) % mod);
10+
}
11+
return s-1;
12+
}
13+
};

minimum-area-rectangle.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Minimum Area Rectangle
2+
class Solution {
3+
public:
4+
int minAreaRect(vector<vector<int>> &points) {
5+
unordered_set<int> s;
6+
int mx = 40001*40001, r = mx;
7+
for (auto &ps : points)
8+
s.insert(ps[0]*40001+ps[1]);
9+
for (auto &ps : points) {
10+
int x0 = ps[0], y0 = ps[1];
11+
for (auto &qs : points) {
12+
int x1 = qs[0], y1 = qs[1];
13+
if (x0 != x1 && y0 != y1) {
14+
if (s.count(x0*40001+y1) && s.count(x1*40001+y0))
15+
r = min(r, abs(x0-x1)*abs(y0-y1));
16+
}
17+
}
18+
}
19+
return r == mx ? 0 : r;
20+
}
21+
};

range-sum-of-bst.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Range Sum of BST
2+
class Solution {
3+
public:
4+
int rangeSumBST(TreeNode *root, int L, int R) {
5+
if (!root) return 0;
6+
int v = root->val;
7+
return (L<=v&&v<=R?v:0) + rangeSumBST(root->left, L, R) + rangeSumBST(root->right, L, R);
8+
}
9+
};

reorder-log-files.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Reorder Log Files
2+
class Solution {
3+
public:
4+
vector<string> reorderLogFiles(vector<string> &logs) {
5+
int n = logs.size();
6+
vector<array<string, 3>> a;
7+
for (auto &log : logs) {
8+
int t = log.find(' ');
9+
a.push_back({log.substr(t+1), log.substr(0, t), log});
10+
}
11+
stable_sort(a.begin(), a.end(), [&](auto &l, auto &r) {
12+
int dl = l[0][0] <= '9', dr = r[0][0] <= '9';
13+
if (dl != dr) return dl < dr;
14+
if (dl) return false;
15+
return l[0] != r[0] ? l[0] < r[0] : l[1] < r[1];
16+
});
17+
vector<string> r;
18+
for (auto &x: a)
19+
r.push_back(x[2]);
20+
return r;
21+
}
22+
};

0 commit comments

Comments
 (0)