Skip to content

Commit

Permalink
Create make-a-square-with-the-same-color.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Apr 28, 2024
1 parent 487c7f0 commit 130ebda
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions C++/make-a-square-with-the-same-color.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Time: O((n - w + 1)^2 * w^2)
// Space: O(1)

// array
class Solution {
public:
bool canMakeSquare(vector<vector<char>>& grid) {
static const int N = 3, W = 2;

for (int i = 0; i < N - W + 1; ++i) {
for (int j = 0; j < N - W + 1; ++j) {
vector<int> cnt(2);
for (int h = 0; h < W; ++h) {
for (int w = 0; w < W; ++w) {
++cnt[grid[i + h][j + w] == 'W' ? 0 : 1];
}
}
if (ranges::max(cnt) >= W * W - 1) {
return true;
}
}
}
return false;
}
};

0 comments on commit 130ebda

Please sign in to comment.