Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4c21a2c

Browse files
committed
misc
1 parent 5e9f54f commit 4c21a2c

33 files changed

Lines changed: 783 additions & 0 deletions

calculate-digit-sum-of-a-string.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Calculate Digit Sum of a String
2+
class Solution {
3+
public:
4+
string digitSum(string s, int k) {
5+
while (s.size() > k) {
6+
int n = s.size();
7+
string ss;
8+
for (int j = 0; j < n; ) {
9+
int i = j, x = 0;
10+
for (; j < min(i+k, n); j++)
11+
x += s[j]-'0';
12+
ss += to_string(x);
13+
}
14+
s = ss;
15+
}
16+
return s;
17+
}
18+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Check if There is a Valid Partition For The Array
2+
class Solution {
3+
public:
4+
bool validPartition(vector<int>& a) {
5+
int n = a.size();
6+
vector<char> f(n+1);
7+
f[0] = 1;
8+
for (int i = 2, t; i <= n; i++) {
9+
if (a[i-2] == a[i-1])
10+
f[i] = f[i-2];
11+
if (i >= 3 && (t = a[i-1]-a[i-2], (t==0||t==1) && a[i-2]-a[i-3] == t))
12+
f[i] |= f[i-3];
13+
}
14+
return f[n];
15+
}
16+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Construct Smallest Number From DI String
2+
class Solution {
3+
public:
4+
string smallestNumber(string p) {
5+
int n = p.size();
6+
string c = "987654321", s;
7+
for (int i = 0; i < n; ) {
8+
if (p[i] == 'I') {
9+
s += c.back();
10+
c.pop_back();
11+
i++;
12+
continue;
13+
}
14+
int j = i;
15+
while (++i < n && p[i] == 'D');
16+
s += c.substr(c.size()-1-(i-j), i-j);
17+
c.erase(c.begin()+c.size()-1-(i-j), c.end()-1);
18+
}
19+
return s+c.back();
20+
}
21+
};

count-collisions-on-a-road.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Count Collisions on a Road
2+
class Solution {
3+
public:
4+
int countCollisions(string a) {
5+
int n = a.size(), i = 0;
6+
while (i < n && a[i] == 'L') i++;
7+
while (i < n && a[n-1] == 'R') n--;
8+
return n-i-count(a.begin()+i, a.begin()+n, 'S');
9+
}
10+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Count Hills and Valleys in an Array
2+
class Solution {
3+
public:
4+
int countHillValley(vector<int>& a) {
5+
int s = 0, n = a.size();
6+
for (int j = 0, i = 0; i < n; i = j) {
7+
for (; j < n && a[i] == a[j]; j++);
8+
if (i && j < n && (a[i]>a[i-1]) == (a[i]>a[j]))
9+
s++;
10+
}
11+
return s;
12+
}
13+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Count Lattice Points Inside a Circle
2+
#define ALL(x) (x).begin(), (x).end()
3+
#define FOR(i, a, b) for (long i = (a); i < (b); i++)
4+
using pii = pair<int, int>;
5+
6+
class Solution {
7+
public:
8+
int countLatticePoints(vector<vector<int>>& circles) {
9+
const int R = 100;
10+
vector<vector<pii>> as(3*R+1);
11+
for (auto &cir: circles) {
12+
int x0 = cir[0], y0 = cir[1], r = cir[2];
13+
FOR(x, x0-r, x0+r+1) {
14+
int s = r*r-(x-x0)*(x-x0), d = sqrt(s+1e-6);
15+
as[x+R].emplace_back(y0-d, 1);
16+
as[x+R].emplace_back(y0+d+1, -1);
17+
}
18+
}
19+
int ret = 0;
20+
for (auto &a : as) {
21+
sort(ALL(a));
22+
int d = 0, bgn = -R-1;
23+
for (auto &x: a) {
24+
if (d == 0)
25+
bgn = x.first;
26+
d += x.second;
27+
if (d == 0)
28+
ret += x.first-bgn;
29+
}
30+
}
31+
return ret;
32+
}
33+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Count Number of Rectangles Containing Each Point
2+
#define ALL(x) (x).begin(), (x).end()
3+
#define REP(i, n) for (long i = 0; i < (n); i++)
4+
5+
class Solution {
6+
vector<int> fenwick;
7+
void add(int n, int x) {
8+
for (; x < n; x |= x+1)
9+
fenwick[x]++;
10+
}
11+
int getSum(int x) {
12+
int s = 0;
13+
for (; x; x &= x-1)
14+
s += fenwick[x-1];
15+
return s;
16+
}
17+
public:
18+
vector<int> countRectangles(vector<vector<int>>& rects, vector<vector<int>>& points) {
19+
int n = points.size(), m = rects.size();
20+
vector<int> ret(n);
21+
REP(i, n)
22+
points[i].push_back(i);
23+
sort(ALL(rects), greater<>());
24+
sort(ALL(points), greater<>());
25+
fenwick.assign(101, 0);
26+
for (int j = 0, i = 0; i < n; i++) {
27+
for (; j < m && points[i][0] <= rects[j][0]; j++)
28+
add(101, rects[j][1]);
29+
ret[points[i][2]] = j - getSum(points[i][1]);
30+
}
31+
return ret;
32+
}
33+
};

count-special-integers.cc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Count Special Integers
2+
#define REP(i, n) for (long i = 0; i < (n); i++)
3+
#define ROF(i, a, b) for (long i = (b); --i >= (a); )
4+
5+
class Solution {
6+
public:
7+
int countSpecialNumbers(int n) {
8+
int m = 0, a[10], u[10] = {};
9+
n++;
10+
do a[m++] = n%10;
11+
while (n /= 10);
12+
int c = a[m-1], r = c-1;
13+
u[c] = 1;
14+
REP(j, m-1)
15+
r *= 9-j;
16+
REP(j, m-1) {
17+
int c = 9;
18+
REP(k, j)
19+
c *= 9-k;
20+
r += c;
21+
}
22+
ROF(i, 0, m-1) {
23+
c = a[i]-accumulate(u, u+a[i], 0);
24+
REP(j, i)
25+
c *= 10-m+i-j;
26+
r += c;
27+
if (u[a[i]]) break;
28+
u[a[i]] = 1;
29+
}
30+
return r;
31+
}
32+
};

couples-holding-hands.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Couples Holding Hands
2+
class Solution {
3+
public:
4+
int minSwapsCouples(vector<int>& row) {
5+
int n = row.size()/2;
6+
vector<int> uf(n, -1);
7+
auto find = [&](int x) {
8+
int r = x, y;
9+
while (uf[r] >= 0) r = uf[r];
10+
for (; x != r; x = y) y = uf[x], uf[x] = r;
11+
return r;
12+
};
13+
for (int i = 0; i < 2*n; i += 2) {
14+
int x = find(row[i]/2), y = find(row[i+1]/2);
15+
if (x == y) continue;
16+
uf[x] += uf[y];
17+
uf[y] = x;
18+
}
19+
int ret = 0;
20+
for (int i = 0; i < n; i++)
21+
ret += uf[i] >= 0;
22+
return ret;
23+
}
24+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Find Players With Zero or One Losses
2+
#define REP(i, n) for (long i = 0; i < (n); i++)
3+
4+
class Solution {
5+
public:
6+
vector<vector<int>> findWinners(vector<vector<int>>& matches) {
7+
vector<int> c(100001);
8+
set<int> id;
9+
for (auto &m : matches) {
10+
c[m[1]]++;
11+
id.insert(m[0]);
12+
}
13+
vector<vector<int>> ret(2);
14+
for (int i : id)
15+
if (!c[i])
16+
ret[0].push_back(i);
17+
REP(i, 100001)
18+
if (c[i] == 1)
19+
ret[1].push_back(i);
20+
return ret;
21+
}
22+
};

0 commit comments

Comments
 (0)