Skip to content

Commit bc76b04

Browse files
committed
Weekly Contest 100
1 parent ad51266 commit bc76b04

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

bitwise-ors-of-subarrays.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Bitwise ORs of Subarrays
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 subarrayBitwiseORs(vector<int>& a) {
8+
vector<int> b[30];
9+
unordered_set<int> s;
10+
int n = a.size();
11+
REP(i, n) {
12+
s.insert(a[i]);
13+
REP(k, 30)
14+
if (a[i]>>k & 1) {
15+
for (int j: b[k])
16+
s.insert(a[j] |= a[i]);
17+
b[k].clear();
18+
} else
19+
b[k].push_back(i);
20+
}
21+
return s.size();
22+
}
23+
};

increasing-order-search-tree.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Increasing Order Search Tree
2+
class Solution {
3+
TreeNode** y;
4+
void f(TreeNode* x) {
5+
if (x) {
6+
f(x->left);
7+
x->left = nullptr;
8+
*y = x;
9+
y = &x->right;
10+
f(x->right);
11+
}
12+
}
13+
public:
14+
TreeNode* increasingBST(TreeNode* root) {
15+
TreeNode* ret;
16+
y = &ret;
17+
f(root);
18+
*y = nullptr;
19+
return ret;
20+
}
21+
};

monotonic-array.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Monotonic Array
2+
class Solution {
3+
public:
4+
bool isMonotonic(vector<int>& A) {
5+
return max(adjacent_find(A.begin(), A.end(), less<>()), adjacent_find(A.begin(), A.end(), greater<>())) == A.end();
6+
}
7+
};

orderly-queue.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Orderly Queue
2+
class Solution {
3+
public:
4+
string orderlyQueue(string S, int K) {
5+
if (K > 1)
6+
sort(S.begin(), S.end());
7+
else {
8+
string r = S;
9+
for (auto _: S)
10+
S = min(S, r = r.substr(1) + r[0]);
11+
}
12+
return S;
13+
}
14+
};

0 commit comments

Comments
 (0)