Skip to content

Commit baafff8

Browse files
authored
Merge pull request doocs#239 from hoooga/master
add cpp solutions for weekly contest 165 problems (1275-1278)
2 parents 83a60a1 + 800421b commit baafff8

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
bool checkWin(vector<vector<char>>& board, bool moveA) {
3+
char symbol = (moveA ? 'X' : 'O');
4+
for (int i = 0; i < 3; i++)
5+
if (board[i][0] == symbol && board[i][0] == board[i][1] && board[i][0] == board[i][2])
6+
return true;
7+
for (int i = 0; i < 3; i++)
8+
if (board[0][i] == symbol && board[0][i] == board[1][i] && board[0][i] == board[2][i])
9+
return true;
10+
if (board[0][0] == symbol && board[1][1] == symbol && board[2][2] == symbol)
11+
return true;
12+
if (board[0][2] == symbol && board[1][1] == symbol && board[2][0] == symbol)
13+
return true;
14+
return false;
15+
}
16+
public:
17+
string tictactoe(vector<vector<int>>& moves) {
18+
vector<vector<char>> board(3, vector<char>(3, ' '));
19+
bool moveA = true;
20+
for (auto &v : moves) {
21+
board[v[0]][v[1]] = (moveA ? 'X' : 'O');
22+
if (checkWin(board, moveA))
23+
return (moveA ? "A" : "B");
24+
moveA = !moveA;
25+
}
26+
if (moves.size() == 9)
27+
return "Draw";
28+
return "Pending";
29+
}
30+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
vector<int> numOfBurgers(int tomatoSlices, int cheeseSlices) {
4+
int x = (tomatoSlices - 2*cheeseSlices);
5+
if (x < 0 || x % 2 == 1)
6+
return {};
7+
x /= 2;
8+
int y = cheeseSlices - x;
9+
if (y < 0)
10+
return {};
11+
return {x, y};
12+
}
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
int countSquares(vector<vector<int>>& matrix) {
4+
int m = matrix.size(), n = matrix[0].size();
5+
vector<vector<int>> dp = matrix;
6+
for (int i = 0; i < m; i ++) {
7+
for (int j = 0; j < n; j++) {
8+
if (dp[i][j] && i > 0 && j > 0) {
9+
int edge = min(dp[i][j-1], dp[i-1][j]);
10+
if (edge > 0) {
11+
if (matrix[i-edge][j-edge]) {
12+
dp[i][j] = edge + 1;
13+
} else {
14+
dp[i][j] = edge;
15+
}
16+
}
17+
}
18+
}
19+
}
20+
int ans = 0;
21+
for (int i = 0; i < m; i++) {
22+
for (int j = 0; j < n; j++) {
23+
ans += dp[i][j];
24+
}
25+
}
26+
return ans;
27+
}
28+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
int palindromePartition(string s, int k) {
4+
int len = s.length();
5+
// cost[i][j] = min #changes needed to turn (s[i],...,s[i+j]) into a palindrome
6+
vector<vector<int>> cost(len, vector<int>(len));
7+
for (int i = 0; i < len; i++) {
8+
for (int j = i; j < len; j++) {
9+
int begin = i, end = j, cnt = 0;
10+
while(begin < end) {
11+
if (s[begin] != s[end])
12+
cnt++;
13+
begin ++, end--;
14+
}
15+
cost[i][j-i] = cnt;
16+
}
17+
}
18+
// dp[i][j] = min #changes needed to split (s[i],...,s[len]) into j+1 palindromes
19+
vector<vector<int>> dp(len, vector<int>(k, INT_MAX));
20+
for (int i = 0; i < len; i++) {
21+
dp[i][0] = cost[i][len-1-i];
22+
}
23+
for (int kk = 1; kk < k; kk++) {
24+
for (int i = 0; i < len; i++) {
25+
for (int j = i+1; j+kk-1 < len; j++) {
26+
dp[i][kk] = min(dp[i][kk], dp[j][kk-1] + cost[i][j-i-1]);
27+
}
28+
}
29+
}
30+
return dp[0][k-1];
31+
}
32+
};

0 commit comments

Comments
 (0)