Skip to content

Commit d0684c6

Browse files
committed
Weekly Contest 101
1 parent bc76b04 commit d0684c6

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Numbers At Most N Given Digit Set
2+
#define ALL(x) (x).begin(), (x).end()
3+
4+
class Solution {
5+
public:
6+
int atMostNGivenDigitSet(vector<string>& D, int N) {
7+
long c = 1, r = 0, s = 0, ss = 1;
8+
vector<int> ds;
9+
transform(ALL(D), back_inserter(ds), [](auto& x) { return stoi(x); });
10+
for(;;) {
11+
int n = N%10;
12+
s = 0;
13+
for (int d: ds)
14+
if (d < n)
15+
s += c;
16+
else if (d == n)
17+
s += ss;
18+
ss = s;
19+
if (!(N /= 10)) break;
20+
r += c *= ds.size();
21+
}
22+
return s+r;
23+
}
24+
};

online-stock-span.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Online Stock Span
2+
class StockSpanner {
3+
vector<pair<int, int>> s;
4+
public:
5+
StockSpanner() {}
6+
int next(int price) {
7+
int w = 1;
8+
for (; s.size() && s.back().first <= price; s.pop_back())
9+
w += s.back().second;
10+
s.emplace_back(price, w);
11+
return w;
12+
}
13+
};

rle-iterator.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RLE Iterator
2+
class RLEIterator {
3+
vector<int> A;
4+
int i = 0, c = 0;
5+
public:
6+
RLEIterator(vector<int> A) : A(A) {}
7+
8+
int next(int n) {
9+
while (i < A.size()) {
10+
if ((c += n) <= A[i])
11+
return A[i+1];
12+
n = c-A[i];
13+
c = 0;
14+
i += 2;
15+
}
16+
return -1;
17+
}
18+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Valid Permutations for DI Sequence
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); )
5+
6+
class Solution {
7+
static const int P = 1000000007;
8+
public:
9+
int numPermsDISequence(string S) {
10+
int n = S.size();
11+
vector<int> f(n+1);
12+
f[0] = 1;
13+
REP(i, n)
14+
if (S[i] == 'D')
15+
ROF(j, 0, i)
16+
(f[j] += f[j+1]) %= P;
17+
else {
18+
int s = 0;
19+
REP(j, i+2) {
20+
int g = f[j];
21+
f[j] = s;
22+
(s += g) %= P;
23+
}
24+
}
25+
return accumulate(f.begin(), f.end(), 0L) % P;
26+
}
27+
};

0 commit comments

Comments
 (0)