Skip to content

Commit 87777f7

Browse files
committed
Weekly Contest 162
1 parent d43fa88 commit 87777f7

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Cells with Odd Values in a Matrix
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 oddCells(int n, int m, vector<vector<int>>& indices) {
8+
vector<int> row(n), col(m);
9+
for (auto &ind : indices) {
10+
row[ind[0]] ^= 1;
11+
col[ind[1]] ^= 1;
12+
}
13+
int a[2] = {}, b[2] = {};
14+
REP(i, n) a[row[i]]++;
15+
REP(i, m) b[col[i]]++;
16+
return a[0]*b[1]+a[1]*b[0];
17+
}
18+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Maximum Score Words Formed by Letters
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 maxScoreWords(vector<string>& words, vector<char>& letters, vector<int>& score) {
8+
int cnt[26] = {}, g[26] = {};
9+
for (char c: letters) cnt[c-'a']++;
10+
int n = words.size();
11+
int use[30][26] = {}, ans = 0;
12+
REP(i, words.size())
13+
for (char c: words[i])
14+
use[i][c-'a']++;
15+
REP(m, 1<<n) {
16+
fill_n(g, 26, 0);
17+
REP(i, n)
18+
if (m>>i&1)
19+
REP(j, 26)
20+
g[j] += use[i][j];
21+
int s = 0;
22+
REP(j, 26)
23+
if (g[j] > cnt[j])
24+
goto nxt;
25+
REP(j, 26)
26+
s += score[j] * g[j];
27+
ans = max(ans, s);
28+
nxt:;
29+
}
30+
return ans;
31+
}
32+
};

number-of-closed-islands.cc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Number of Closed Islands
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 closedIsland(vector<vector<int>>& a) {
8+
const int dx[] = {0,-1,0,1};
9+
const int dy[] = {1,0,-1,0};
10+
int n = a.size(), m = a[0].size(), x, y, ans = 0;
11+
vector<pair<int, int>> s;
12+
REP(i, n) REP(j, m)
13+
if (!a[i][j]++) {
14+
int b = 0, x = i, y = j;
15+
for(;;) {
16+
REP(d, 4) {
17+
unsigned xx = x+dx[d], yy = y+dy[d];
18+
if (xx >= n || yy >= m)
19+
b = 1;
20+
else if (!a[xx][yy]++)
21+
s.emplace_back(xx, yy);
22+
}
23+
if (s.empty()) break;
24+
std::tie(x, y) = s.back();
25+
s.pop_back();
26+
}
27+
ans += !b;
28+
}
29+
return ans;
30+
}
31+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Reconstruct a 2-Row Binary Matrix
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+
vector<vector<int>> reconstructMatrix(int upper, int lower, vector<int>& colsum) {
8+
int n = colsum.size();
9+
vector<vector<int>> a(2, vector<int>(n));
10+
int one = 0;
11+
REP(i, colsum.size()) {
12+
int x = colsum[i];
13+
if (x==2) a[0][i] = a[1][i] = 1, upper--, lower--;
14+
else if (x==1) one++;
15+
else if (x) return {};
16+
}
17+
if (lower<0 || upper<0 || lower+upper!=one) return {};
18+
REP(i, colsum.size())
19+
if (colsum[i]==1) {
20+
if (lower) a[1][i] = 1, lower--;
21+
else a[0][i] = 1;
22+
}
23+
return a;
24+
}
25+
};

0 commit comments

Comments
 (0)