Skip to content

Commit 24079c9

Browse files
committed
Weekly Contest 129, 130
1 parent adce2a5 commit 24079c9

8 files changed

+117
-0
lines changed

best-sightseeing-pair.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Best Sightseeing Pair
2+
class Solution {
3+
public:
4+
int maxScoreSightseeingPair(vector<int>& A) {
5+
int i = 0, l = 0, r = 0;
6+
for (int x: A) {
7+
r = max(r, l+x-i);
8+
l = max(l, x+i);
9+
i++;
10+
}
11+
return r;
12+
}
13+
};

binary-prefix-divisible-by-5.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Binary Prefix Divisible By 5
2+
class Solution {
3+
public:
4+
vector<bool> prefixesDivBy5(vector<int>& A) {
5+
vector<bool> r;
6+
int s = 0;
7+
for (int x: A) {
8+
s = (s*2+x)%5;
9+
r.push_back(!s);
10+
}
11+
return r;
12+
}
13+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Binary String With Substrings Representing 1 To N
2+
class Solution {
3+
public:
4+
bool queryString(string S, int N) {
5+
if (S.size()*2 < N) return false;
6+
for (; N; N--) {
7+
string a;
8+
for (int i = N; i; i /= 2)
9+
a += '0'+i%2;
10+
reverse(a.begin(), a.end());
11+
if (S.find(a) == string::npos)
12+
return false;
13+
}
14+
return true;
15+
}
16+
};

convert-to-base-2.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Convert to Base -2
2+
class Solution {
3+
public:
4+
string baseNeg2(int N) {
5+
string s;
6+
while (s += "01"[N&1], N = (N-(N&1))/-2);
7+
reverse(s.begin(), s.end());
8+
return s;
9+
}
10+
};

next-greater-node-in-linked-list.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Next Greater Node In Linked List
2+
class Solution {
3+
public:
4+
vector<int> nextLargerNodes(ListNode* h) {
5+
vector<int> a, s, r;
6+
for (int i = 0; h; h = h->next, i++) {
7+
a.push_back(h->val);
8+
r.push_back(0);
9+
for (; s.size() && a[s.back()] < a[i]; s.pop_back())
10+
r[s.back()] = a[i];
11+
s.push_back(i);
12+
}
13+
return r;
14+
}
15+
};

number-of-enclaves.cc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Number of Enclaves
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 numEnclaves(vector<vector<int>>& A) {
8+
int m = A.size(), n = A[0].size(), one = 0, x, y;
9+
vector<pair<int, int>> q;
10+
REP(i, m)
11+
REP(j, n)
12+
if (A[i][j]) {
13+
one++;
14+
if (!i || !j || i==m-1 || j==n-1) {
15+
A[i][j] = 0;
16+
q.emplace_back(i, j);
17+
}
18+
}
19+
REP(i, q.size()) {
20+
tie(x, y) = q[i];
21+
if (x && !--A[x-1][y]) q.emplace_back(x-1, y);
22+
if (x+1<m && !--A[x+1][y]) q.emplace_back(x+1, y);
23+
if (y && !--A[x][y-1]) q.emplace_back(x, y-1);
24+
if (y+1<n && !--A[x][y+1]) q.emplace_back(x, y+1);
25+
}
26+
return one - q.size();
27+
}
28+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Partition Array Into Three Parts With Equal Sum
2+
class Solution {
3+
public:
4+
bool canThreePartsEqualSum(vector<int>& A) {
5+
int e = accumulate(A.begin(), A.end(), 0)/3, s = 0, c = 0;
6+
for (int x: A)
7+
if ((s += x) == e)
8+
s = 0, c++;
9+
return c >= 3;
10+
}
11+
};

smallest-integer-divisible-by-k.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Smallest Integer Divisible by K
2+
class Solution {
3+
public:
4+
int smallestRepunitDivByK(int K) {
5+
int s = 0;
6+
for (int i = 0; i < K; i++)
7+
if (!(s = (s*10+1)%K))
8+
return i+1;
9+
return -1;
10+
}
11+
};

0 commit comments

Comments
 (0)