Skip to content

Commit 5593300

Browse files
committed
Weekly Contest 93
1 parent 9a2b4ff commit 5593300

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

advantage-shuffle.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Advantage Shuffle
2+
#define ALL(x) (x).begin(), (x).end()
3+
4+
class Solution {
5+
public:
6+
vector<int> advantageCount(vector<int>& A, vector<int>& B) {
7+
int n = A.size();
8+
vector<int> p(n), r(n);
9+
iota(ALL(p), 0);
10+
sort(ALL(p), [&](int x, int y) { return B[x] < B[y]; });
11+
sort(ALL(A));
12+
for (int i = n, j = n, k = 0, q = n; i--; ) {
13+
while (j > k && A[j-1] > B[p[i]])
14+
A[--q] = A[--j];
15+
r[p[i]] = q < n ? A[q++] : A[k++];
16+
}
17+
return r;
18+
}
19+
};

binary-gap.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Binary Gap
2+
class Solution {
3+
public:
4+
int binaryGap(int N) {
5+
int s = 0, j = -1;
6+
for (int i = 0; i < 30; i++)
7+
if (N>>i & 1) {
8+
if (~j)
9+
s = max(s, i - j);
10+
j = i;
11+
}
12+
return s;
13+
}
14+
};

minimum-number-of-refueling-stops.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Minimum Number of Refueling Stops
2+
class Solution {
3+
public:
4+
int minRefuelStops(int target, int d, vector<vector<int>>& ss) {
5+
int r = 0, i = 0, n = ss.size();
6+
priority_queue<int> pq;
7+
while (d < target) {
8+
while (i < n && ss[i][0] <= d)
9+
pq.push(ss[i++][1]);
10+
if (pq.empty()) return -1;
11+
d += pq.top();
12+
pq.pop();
13+
r++;
14+
}
15+
return r;
16+
}
17+
};

reordered-power-of-2.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Reordered Power of 2
2+
class Solution {
3+
public:
4+
bool reorderedPowerOf2(int N) {
5+
int ten[10] = {}, ten1[10] = {};
6+
do ten[N%10]++;
7+
while (N /= 10);
8+
for (int m = 1<<30; m; m >>= 1) {
9+
copy_n(ten, 10, ten1);
10+
int t = m;
11+
do ten1[t%10]--;
12+
while (t /= 10);
13+
if (count(ten1, ten1+10, 0) == 10) return true;
14+
}
15+
return false;
16+
}
17+
};

0 commit comments

Comments
 (0)