Skip to content

Commit 306830e

Browse files
authored
Create count-square-submatrices-with-all-ones.cpp
1 parent d538be3 commit 306830e

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Time: O(m * n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int countSquares(vector<vector<int>>& matrix) {
7+
for (int i = 1; i < matrix.size(); ++i) {
8+
for (int j = 1; j < matrix[0].size(); ++j) {
9+
if (!matrix[i][j]) {
10+
continue;
11+
}
12+
int l = min(matrix[i - 1][j], matrix[i][j - 1]);
13+
matrix[i][j] = matrix[i - l][j - l] ? l + 1 : l;
14+
}
15+
}
16+
int result = 0;
17+
for (const auto& row : matrix) {
18+
for (const auto& x : row) {
19+
result += x;
20+
}
21+
}
22+
return result;
23+
}
24+
};

0 commit comments

Comments
 (0)