Skip to content

Commit adce2a5

Browse files
committed
Weekly Contest 128
1 parent ac01e34 commit adce2a5

4 files changed

+67
-0
lines changed
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Capacity To Ship Packages Within D Days
2+
class Solution {
3+
public:
4+
int shipWithinDays(vector<int>& ws, int D) {
5+
long l = *max_element(ws.begin(), ws.end()), h = ws.size()*500;
6+
while (l < h) {
7+
long m = l+h >> 1, c = 0, x = 0;
8+
for (int w: ws)
9+
if ((x += w) > m)
10+
c++, x = w;
11+
if (c < D) h = m;
12+
else l = m+1;
13+
}
14+
return l;
15+
}
16+
};

complement-of-base-10-integer.cc

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Complement of Base 10 Integer
2+
class Solution {
3+
public:
4+
int bitwiseComplement(int N) {
5+
return N ? (1 << 32-__builtin_clz(N)) - 1 - N : 1;
6+
}
7+
};

numbers-with-1-repeated-digit..cc

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Numbers With 1 Repeated Digit
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+
public:
8+
int numDupDigitsAtMostN(int N) {
9+
int m = 0, n = N+1, a[10], u[10] = {};
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 N-r;
31+
}
32+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Pairs of Songs With Total Durations Divisible by 60
2+
class Solution {
3+
public:
4+
int numPairsDivisibleBy60(vector<int> &time) {
5+
int c[60] = {}, r = 0;
6+
for (int x : time) {
7+
r += c[x%60];
8+
c[(60-x%60)%60]++;
9+
}
10+
return r;
11+
}
12+
};

0 commit comments

Comments
 (0)