Skip to content

Commit 7a1f87b

Browse files
committed
misc
1 parent 1502c88 commit 7a1f87b

21 files changed

+395
-139
lines changed

3sum-closest.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Solution {
55
public:
66
int threeSumClosest(vector<int> &a, int target) {
77
int n = a.size(), opt = INT_MAX, opts;
8-
sort(a.begin(), a.end());
8+
ranges::sort(a);
99
REP(i, n) {
1010
int j = i+1, k = n-1, t = target-a[i];
1111
while (j < k) {

3sum.cc

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
// 3Sum
22
class Solution {
33
public:
4-
vector<vector<int> > threeSum(vector<int> &a) {
4+
vector<vector<int>> threeSum(vector<int>& a) {
55
int n = a.size();
66
vector<vector<int>> r;
7-
sort(a.begin(), a.end());
7+
ranges::sort(a);
88
for (int i = 0; i < n; ) {
9-
int j = i+1, k = n-1, s = -a[i], old;
9+
int j = i+1, k = n-1, s = -a[i];
1010
while (j < k) {
11-
if (a[j]+a[k] < s) j++;
12-
else if (a[j]+a[k] > s) k--;
13-
else {
14-
r.push_back(vector<int>{a[i], a[j], a[k]});
15-
old = a[j];
16-
while (++j < k && a[j] == old);
11+
if (a[j]+a[k] > s)
1712
k--;
13+
else {
14+
if (a[j]+a[k] == s)
15+
r.push_back({a[i], a[j], a[k]});
16+
while (++j < k && a[j] == a[j-1]);
1817
}
1918
}
20-
old = a[i];
21-
while (++i < n && a[i] == old);
19+
while (++i < n && a[i] == a[i-1]);
2220
}
2321
return r;
2422
}

4sum.cc

+15-26
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,41 @@
11
// 4Sum
22
class Solution {
33
public:
4-
vector<vector<int> > fourSum(vector<int> &a, int target) {
4+
vector<vector<int>> fourSum(vector<int> &a, int target) {
55
int n = a.size(), old;
6+
const long tar = target;
67
multimap<int, int> m;
78
vector<vector<int>> r;
89
sort(a.begin(), a.end());
910
for (int i = 0; i < n; ) {
1011
// a <= b < c <= d
1112
for (int j = i+1; j < n; ) {
12-
int t = target-a[i]-a[j];
13+
long t = tar-a[i]-a[j];
1314
auto it = m.equal_range(t);
14-
for (; it.first != it.second; ++it.first) {
15-
vector<int> b{it.first->second, t-it.first->second, a[i], a[j]};
16-
r.push_back(b);
17-
}
18-
old = a[j];
19-
while (++j < n && a[j] == old);
15+
for (; it.first != it.second; ++it.first)
16+
r.push_back({it.first->second, (int)t-it.first->second, a[i], a[j]});
17+
while (++j < n && a[j] == a[j-1]);
2018
}
2119
// a < b = b <= c
2220
if (i+1 < n && a[i] == a[i+1]) {
2321
for (int j = i+2; j < n; ) {
24-
int t = target-a[i]*2-a[j];
25-
auto it = lower_bound(a.begin(), a.begin()+i, t);
26-
if (it != a.begin()+i && *it == t) {
27-
vector<int> b{*it, a[i], a[i], a[j]};
28-
r.push_back(b);
29-
}
30-
old = a[j];
31-
while (++j < n && a[j] == old);
22+
long t = tar-a[i]*2-a[j];
23+
if (binary_search(a.begin(), a.begin()+i, t))
24+
r.push_back({int(t), a[i], a[i], a[j]});
25+
while (++j < n && a[j] == a[j-1]);
3226
}
3327
}
3428
// a = a = a <= b
3529
if (i+2 < n && a[i] == a[i+2]) {
36-
int t = target-a[i]*3;
37-
auto it = lower_bound(a.begin()+i+3, a.end(), t);
38-
if (it != a.end() && *it == t) {
39-
vector<int> b{a[i], a[i], a[i], t};
40-
r.push_back(b);
41-
}
30+
long t = tar-a[i]*3L;
31+
if (binary_search(a.begin()+i+3, a.end(), t))
32+
r.push_back({a[i], a[i], a[i], int(t)});
4233
}
43-
old = a[i];
44-
while (i+1 < n && a[i+1] == old)
34+
while (i+1 < n && a[i+1] == a[i])
4535
i++;
4636
for (int j = 0; j < i; ) {
4737
m.insert(make_pair(a[j]+a[i], a[j]));
48-
old = a[j];
49-
while (++j < n && a[j] == old);
38+
while (++j < n && a[j] == a[j-1]);
5039
}
5140
i++;
5241
}

count-the-number-of-good-nodes.cc

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Count the Number of Good Nodes
2+
class Solution {
3+
int n = 0, res = 0;
4+
vector<vector<int>> es;
5+
int dfs(int u, int p) {
6+
int s = 1, t0 = 0, ok = 1;
7+
for (int v : es[u])
8+
if (v != p) {
9+
int t = dfs(v, u);
10+
s += t;
11+
if (t0 && t != t0)
12+
ok = 0;
13+
t0 = t;
14+
}
15+
res += ok;
16+
return s;
17+
}
18+
public:
19+
int countGoodNodes(vector<vector<int>>& edges) {
20+
for (auto &e : edges)
21+
n = max(n, max(e[0], e[1])+1);
22+
es.resize(n);
23+
for (auto &e : edges) {
24+
es[e[0]].push_back(e[1]);
25+
es[e[1]].push_back(e[0]);
26+
}
27+
dfs(0, -1);
28+
return res;
29+
}
30+
};

divide-two-integers.cc

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class Solution {
1515
p -= q;
1616
r += 1 << c;
1717
}
18-
return neg ? -r : r;
18+
if (r == INT_MIN && !neg)
19+
return INT_MAX;
20+
return neg ? -(unsigned)r : r;
1921
}
2022
};
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Find the Count of Monotonic Pairs II
2+
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
3+
#define REP(i, n) for (int i = 0; i < (n); i++)
4+
5+
const int M = 1001, P = 1000000007;
6+
7+
class Solution {
8+
public:
9+
int countOfPairs(vector<int>& a) {
10+
int n = a.size();
11+
vector<int> s0(M), s1(M);
12+
fill_n(&s0[0], a[0]+1, 1);
13+
REP(i, n-1) {
14+
REP(j, a[i]+1) {
15+
if (!s0[j])
16+
continue;
17+
int lo = max(j, a[i+1]-a[i]+j), hi = a[i+1]+1;
18+
if (lo < hi) {
19+
(s1[lo] += s0[j]) %= P;
20+
if (hi < M) (s1[hi] -= s0[j]) %= P;
21+
}
22+
}
23+
s0[0] = exchange(s1[0], 0);
24+
FOR(j, 1, M)
25+
s0[j] = (s0[j-1] + exchange(s1[j], 0))%P;
26+
}
27+
int ret = 0;
28+
REP(j, M)
29+
(ret += s0[j]) %= P;
30+
return (ret%P+P)%P;
31+
}
32+
};

find-the-count-of-monotonic-pairs.cc

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Find the Count of Monotonic Pairs I
2+
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
3+
#define REP(i, n) for (int i = 0; i < (n); i++)
4+
5+
const int M = 1001, P = 1000000007;
6+
7+
class Solution {
8+
public:
9+
int countOfPairs(vector<int>& a) {
10+
int n = a.size();
11+
vector<int> s0(M), s1(M);
12+
fill_n(&s0[0], a[0]+1, 1);
13+
REP(i, n-1) {
14+
REP(j, a[i]+1) {
15+
if (!s0[j])
16+
continue;
17+
int lo = max(j, a[i+1]-a[i]+j), hi = a[i+1]+1;
18+
if (lo < hi) {
19+
(s1[lo] += s0[j]) %= P;
20+
if (hi < M) (s1[hi] -= s0[j]) %= P;
21+
}
22+
}
23+
s0[0] = exchange(s1[0], 0);
24+
FOR(j, 1, M)
25+
s0[j] = (s0[j-1] + exchange(s1[j], 0))%P;
26+
}
27+
int ret = 0;
28+
REP(j, M)
29+
(ret += s0[j]) %= P;
30+
return (ret%P+P)%P;
31+
}
32+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Find the Index of the First Occurrence in a String
2+
class Solution {
3+
public:
4+
int strStr(string haystack, string needle) {
5+
for (int i = 0; i <= haystack.size()-needle.size(); i++)
6+
if (!haystack.compare(i, needle.size(), needle))
7+
return i;
8+
return -1;
9+
}
10+
};

find-the-value-of-the-partition.cc

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Find the Value of the Partition
2+
#define ALL(x) (x).begin(), (x).end()
3+
#define REP(i, n) for (int i = 0; i < (n); i++)
4+
5+
class Solution {
6+
public:
7+
int findValueOfPartition(vector<int>& a) {
8+
int s = INT_MAX;
9+
sort(ALL(a));
10+
REP(i, a.size()-1)
11+
s = min(s, a[i+1]-a[i]);
12+
return s;
13+
}
14+
};

finding-mk-average.cc

+25-25
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,9 @@ struct Treap { int lc, rc, pri, size, key; long sum; };
216216
unique_ptr<Treap[]> tr;
217217

218218
void mconcat(int x) {
219-
tr[x].size = tr[tr[x].lc].size + 1 + tr[tr[x].rc].size;
220-
tr[x].sum = tr[tr[x].lc].sum + tr[x].key + tr[tr[x].rc].sum;
219+
int l = tr[x].lc, r = tr[x].rc;
220+
tr[x].size = tr[l].size + 1 + tr[r].size;
221+
tr[x].sum = tr[l].sum + tr[x].key + tr[r].sum;
221222
}
222223

223224
void split_by_rank(int x, int k, int &l, int &r) {
@@ -235,7 +236,7 @@ void split_by_rank(int x, int k, int &l, int &r) {
235236

236237
void split_by_key(int x, int key, int &l, int &r) {
237238
if (!x) return void(l = r = 0);
238-
if (key <= tr[x].key) {
239+
if (key < tr[x].key) {
239240
r = x;
240241
split_by_key(tr[x].lc, key, l, tr[x].lc);
241242
} else {
@@ -247,52 +248,51 @@ void split_by_key(int x, int key, int &l, int &r) {
247248

248249
int join(int x, int y) {
249250
if (!x || !y) return x^y;
250-
if (tr[x].pri < tr[y].pri) {
251+
if (tr[x].pri < tr[y].pri)
251252
tr[x].rc = join(tr[x].rc, y);
252-
mconcat(x);
253-
return x;
254-
} else {
253+
else {
255254
tr[y].lc = join(x, tr[y].lc);
256-
mconcat(y);
257-
return y;
255+
x = y;
258256
}
257+
mconcat(x);
258+
return x;
259259
}
260260
}
261261

262262
class MKAverage {
263-
int m, k, id, root = 0;
263+
int m, k, root = 0;
264264
deque<int> q;
265-
266-
int erase(int x, int k) {
267-
if (k == tr[x].key) {
268-
int ret = join(tr[x].lc, tr[x].rc);
269-
id = x;
265+
int erase(int &x, int key) {
266+
if (tr[x].key == key) {
267+
int ret = x;
268+
x = join(tr[x].lc, tr[x].rc);
270269
return ret;
271270
}
272-
if (k < tr[x].key)
273-
tr[x].lc = erase(tr[x].lc, k);
271+
int id;
272+
if (key < tr[x].key)
273+
id = erase(tr[x].lc, key);
274274
else
275-
tr[x].rc = erase(tr[x].rc, k);
275+
id = erase(tr[x].rc, key);
276276
mconcat(x);
277-
return x;
277+
return id;
278278
}
279279
public:
280-
MKAverage(int m, int k) : m(m), k(k) { tr = make_unique<Treap[]>(m+1); }
280+
MKAverage(int m, int k) : m(m), k(k) {
281+
tr = make_unique<Treap[]>(m+1);
282+
}
281283

282284
void addElement(int num) {
283-
int l, r;
284-
id = q.size()+1;
285+
int l, r, id = q.size()+1;
285286
if (q.size() == m) {
286-
int key = q.front();
287+
id = erase(root, q.front());
287288
q.pop_front();
288-
root = erase(root, key);
289289
}
290290
tr[id].lc = tr[id].rc = 0;
291291
tr[id].pri = xor32();
292292
tr[id].size = 1;
293293
tr[id].key = tr[id].sum = num;
294294
split_by_key(root, num, l, r);
295-
root = join(l, join(id, r));
295+
root = join(join(l, id), r);
296296
q.push_back(num);
297297
}
298298

median-of-two-sorted-arrays.cc

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
// Median of Two Sorted Arrays
22
class Solution {
33
public:
4-
double findMedianSortedArrays(vector<int> &a, vector<int> &b) {
5-
int m = a.size(), n = b.size(), i = 0, j = 0, k = m+n-1 >> 1;
6-
while (k > 0) {
7-
int p = k-1 >> 1;
8-
if (j+p >= n || i+p < m && a[i+p] < b[j+p])
9-
i += p+1;
10-
else
11-
j += p+1;
12-
k -= p+1;
13-
}
14-
int s = j >= n || i < m && a[i] < b[j] ? a[i++] : b[j++];
15-
return m+n & 1 ? s : (j >= n || i < m && a[i] < b[j] ? s+a[i] : s+b[j]) * 0.5;
4+
double findMedianSortedArrays(vector<int> &a, vector<int> &b) {
5+
int m = a.size(), n = b.size(), i = 0, j = 0, k = m+n-1 >> 1;
6+
while (k > 0) {
7+
int p = k-1 >> 1;
8+
if (j+p >= n || i+p < m && a[i+p] < b[j+p])
9+
i += p+1;
10+
else
11+
j += p+1;
12+
k -= p+1;
1613
}
14+
int s = j >= n || i < m && a[i] < b[j] ? a[i++] : b[j++];
15+
return m+n & 1 ? s : (j >= n || i < m && a[i] < b[j] ? s+a[i] : s+b[j]) * 0.5;
16+
}
1717
};

0 commit comments

Comments
 (0)