Skip to content

Commit 12264fd

Browse files
committed
misc
1 parent 830fedf commit 12264fd

14 files changed

+254
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Check if Numbers Are Ascending in a Sentence
2+
#define REP(i, n) for (long i = 0; i < (n); i++)
3+
4+
class Solution {
5+
public:
6+
bool areNumbersAscending(string s) {
7+
int last = -1;
8+
REP(i, s.size())
9+
if (s[i] != ' ') {
10+
int j = i, x = atoi(&s[i]);
11+
while (++i < s.size() && s[i] != ' ');
12+
if (isdigit(s[j])) {
13+
if (last >= 0 && last >= x)
14+
return false;
15+
last = x;
16+
}
17+
}
18+
return true;
19+
}
20+
};

count-special-quadruplets.cc

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Count Special Quadruplets
2+
#define FOR(i, a, b) for (long i = (a); i < (b); i++)
3+
#define REP(i, n) for (long i = 0; i < (n); i++)
4+
5+
class Solution {
6+
public:
7+
int countQuadruplets(vector<int>& a) {
8+
int n = a.size(), ans = 0;
9+
REP(i, n)
10+
FOR(j, i+1, n)
11+
FOR(k, j+1, n) {
12+
int x = a[i]+a[j]+a[k];
13+
FOR(l, k+1, n)
14+
if (x == a[l])
15+
ans++;
16+
}
17+
return ans;
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Final Value of Variable After Performing Operations
2+
class Solution {
3+
public:
4+
int finalValueAfterOperations(vector<string>& operations) {
5+
int s = 0;
6+
for (auto op: operations)
7+
if (op[1] == '+') s++;
8+
else s--;
9+
return s;
10+
}
11+
};

find-missing-observations.cc

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Find Missing Observations
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+
public:
7+
vector<int> missingRolls(vector<int>& rolls, int mean, int n) {
8+
int s = mean*(rolls.size()+n)-accumulate(ALL(rolls), 0);
9+
if (s < n || s > 6*n) return {};
10+
vector<int> a(n);
11+
REP(i, n) {
12+
a[i] = s/(n-i);
13+
s -= a[i];
14+
}
15+
return a;
16+
}
17+
};

grid-game.cc

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Grid Game
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-
#define ROF(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (b); --i >= (a); )
2+
#define REP(i, n) for (long i = 0; i < (n); i++)
3+
#define ROF(i, a, b) for (long i = (b); --i >= (a); )
54

65
class Solution {
76
public:
@@ -11,7 +10,7 @@ class Solution {
1110
REP(i, n)
1211
l[i] = grid[1][i] + (i ? l[i-1] : 0);
1312
ROF(i, 0, n)
14-
r[i] = grid[0][i] + (i+1<n ? r[i+1] : 0);
13+
r[i] = grid[0][i] + r[i+1];
1514
REP(i, n)
1615
ans = min(ans, max(r[i+1], i ? l[i-1] : 0));
1716
return ans;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Maximum Value at a Given Index in a Bounded Array
2+
proc maxValue(n: int, index: int, maxSum: int): int {.exportc.} =
3+
var
4+
l = 0
5+
h = maxSum - n
6+
while l < h:
7+
let a = (l+h+1) shr 1
8+
var b = max(a-index, 0)
9+
var sum = (a+b)*(a-b+1)
10+
b = max(a-(n-1)+index, 0)
11+
sum += (a+b)*(a-b+1)
12+
if sum div 2 - a <= maxSum - n:
13+
l = a
14+
else:
15+
h = a-1
16+
l+1

minimum-moves-to-convert-string.cc

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Minimum Moves to Convert String
2+
#define REP(i, n) for (long i = 0; i < (n); i++)
3+
4+
class Solution {
5+
public:
6+
int minimumMoves(string s) {
7+
int n = s.size(), ans = 0;
8+
REP(i, n)
9+
if (s[i] == 'X') {
10+
int j = min((int)i+2, n-1);
11+
s[j] = 'O';
12+
if (j) s[j-1] = 'O';
13+
if (j > 1) s[j-2] = 'O';
14+
ans++;
15+
}
16+
return ans;
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Next Greater Numerically Balanced Number
2+
#define REP(i, n) for (long i = 0; i < (n); i++)
3+
4+
class Solution {
5+
public:
6+
int nextBeautifulNumber(int n) {
7+
int a[9] = {}, c[10] = {}, m = 0;
8+
for (int i = n; i; i /= 10)
9+
c[a[m++] = i%10]++;
10+
for(;;) {
11+
n++;
12+
int i = 0;
13+
for (; ; i++) {
14+
if (i < m)
15+
c[a[i]]--;
16+
else
17+
m++;
18+
if (a[i] < 9) {
19+
c[++a[i]]++;
20+
i++;
21+
break;
22+
}
23+
c[a[i] = 0]++;
24+
}
25+
26+
int j = 0;
27+
for (; j < 10; j++)
28+
if (c[j] && c[j]!=j)
29+
break;
30+
if (j == 10)
31+
return n;
32+
}
33+
}
34+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Number of Pairs of Interchangeable Rectangles
2+
#define ALL(x) (x).begin(), (x).end()
3+
4+
class Solution {
5+
public:
6+
long long interchangeableRectangles(vector<vector<int>>& rs) {
7+
auto a = rs;
8+
for (auto &x: a) {
9+
long d = __gcd(x[0], x[1]);
10+
x[0] /= d;
11+
x[1] /= d;
12+
}
13+
sort(ALL(a));
14+
long ans = 0;
15+
for (long j = 0, i = 0; i != a.size(); i = j) {
16+
for (; j < a.size() && a[i] == a[j]; j++);
17+
ans += (j-i)*(j-i-1)/2;
18+
}
19+
return ans;
20+
}
21+
};
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Number of Valid Words in a Sentence
2+
#define REP(i, n) for (long i = 0; i < (n); i++)
3+
4+
class Solution {
5+
public:
6+
int countValidWords(string a) {
7+
int n = a.size(), ans = 0;
8+
REP(i, n)
9+
if (a[i] != ' ') {
10+
int j = i, hyp = 0, dot = 0, digit = 0;
11+
for (; j < n && a[j] != ' '; j++) {
12+
if (a[j] == '-') {
13+
hyp++;
14+
if (!j || !isalpha(a[j-1]) || !isalpha(a[j+1]))
15+
hyp++;
16+
}
17+
dot += a[j]=='.'||a[j]==','||a[j]=='!';
18+
digit += isdigit(a[j]);
19+
}
20+
if (hyp <= 1 && !digit && (!dot || dot==1 && (a[j-1]=='.'||a[j-1]==','||a[j-1]=='!')))
21+
ans++;
22+
i = j;
23+
}
24+
return ans;
25+
}
26+
};

simple-bank-system.cc

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Simple Bank System
2+
class Bank {
3+
vector<long long> a;
4+
public:
5+
Bank(vector<long long>& balance) : a(move(balance)) {}
6+
bool f(int x) const { return 1 <= x && x <= a.size(); }
7+
8+
bool transfer(int x, int y, long long mo) {
9+
if (!f(x) || !f(y) || a[x-1] < mo) return false;
10+
a[x-1] -= mo;
11+
a[y-1] += mo;
12+
return true;
13+
}
14+
15+
bool deposit(int x, long long mo) {
16+
if (!f(x)) return false;
17+
a[x-1] += mo;
18+
return true;
19+
}
20+
21+
bool withdraw(int x, long long mo) {
22+
if (!f(x) || a[x-1] < mo) return false;
23+
a[x-1] -= mo;
24+
return true;
25+
}
26+
};

smallest-index-with-equal-value.nim

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
proc smallestEqual(nums: ptr UncheckedArray[cint], n: int): cint {.exportc.} =
2+
for i in 0..<n:
3+
if i mod 10 == nums[i]:
4+
return cast[cint](i)
5+
-1

sum-of-beauty-in-the-array.cc

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Sum of Beauty in the Array
2+
#define FOR(i, a, b) for (long i = (a); i < (b); i++)
3+
#define ROF(i, a, b) for (long i = (b); --i >= (a); )
4+
5+
class Solution {
6+
public:
7+
int sumOfBeauties(vector<int>& a) {
8+
int n = a.size(), s = 0, b = a[0];
9+
vector<int> c(n);
10+
c[n-1] = a[n-1];
11+
ROF(i, 0, n-1)
12+
c[i] = min(c[i+1], a[i]);
13+
FOR(i, 1, n-1) {
14+
if (a[i-1] < a[i] && a[i] < a[i+1]) {
15+
if (b < a[i] && a[i] < c[i+1])
16+
s++;
17+
s++;
18+
}
19+
b = max(b, a[i]);
20+
}
21+
return s;
22+
}
23+
};

two-out-of-three.cc

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Two Out of Three
2+
class Solution {
3+
public:
4+
vector<int> twoOutOfThree(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3) {
5+
int a[101] = {};
6+
for (int x: nums1) a[x] |= 1;
7+
for (int x: nums2) a[x] |= 2;
8+
for (int x: nums3) a[x] |= 4;
9+
vector<int> ans;
10+
for (int i = 0; i < sizeof(a)/sizeof(*a); i++)
11+
if (__builtin_popcount(a[i]) > 1)
12+
ans.push_back(i);
13+
return ans;
14+
}
15+
};

0 commit comments

Comments
 (0)