Skip to content

Commit 49bb656

Browse files
committed
Weekly Contest 123
1 parent 9e4c4aa commit 49bb656

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

add-to-array-form-of-integer.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Add to Array-Form of Integer
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+
vector<int> addToArrayForm(vector<int> &A, int K) {
9+
reverse(ALL(A));
10+
int n = A.size();
11+
REP(i, n) {
12+
if (!K) break;
13+
int t = A[i] + K % 10;
14+
K = K / 10 + t / 10;
15+
A[i] = t % 10;
16+
}
17+
for (; K; K /= 10)
18+
A.push_back(K % 10);
19+
reverse(ALL(A));
20+
return A;
21+
}
22+
};

broken-calculator.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Broken Calculator
2+
class Solution {
3+
public:
4+
int brokenCalc(int X, int Y) {
5+
int r = 0;
6+
for (; X < Y; r++)
7+
Y = Y & 1 ? Y+1 : Y/2;
8+
return r+X-Y;
9+
}
10+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Satisfiability of Equality Equations
2+
class Solution {
3+
public:
4+
bool equationsPossible(vector<string> &equations) {
5+
int p[26];
6+
iota(p, p + 26, 0);
7+
auto f = [&](int x) {
8+
while (p[x] != x)
9+
p[x] = p[p[x]], x = p[x];
10+
return x;
11+
};
12+
for (auto &eq : equations)
13+
if (eq[1] == '=')
14+
p[f(eq[0]-'a')] = f(eq[3]-'a');
15+
for (auto &eq : equations)
16+
if (eq[1] == '!' && f(eq[0]-'a') == f(eq[3]-'a'))
17+
return false;
18+
return true;
19+
}
20+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Subarrays with K Different Integers
2+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
3+
#define REP(i, n) FOR(i, 0, n)
4+
5+
class Solution {
6+
public:
7+
int subarraysWithKDistinct(vector<int> &A, int K) {
8+
int n = A.size(), j = 0, jj = 0, r = 0;
9+
vector<int> c(n+1);
10+
REP(i, n) {
11+
while (j < n && K) c[A[j++]]++ || K--;
12+
if (K) break;
13+
jj = max(jj, j);
14+
while (jj < n && c[A[jj]]) jj++;
15+
r += jj-j+1;
16+
--c[A[i]] || K++;
17+
}
18+
return r;
19+
}
20+
};

0 commit comments

Comments
 (0)