-
Notifications
You must be signed in to change notification settings - Fork 13
/
solution.cpp
35 lines (32 loc) · 869 Bytes
/
solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <vector>
#include <algorithm>
class Solution
{
public:
int countSquares(std::vector<std::vector<int>> &matrix)
{
int m = matrix.size();
int n = matrix[0].size();
std::vector<std::vector<int>> dp(m, std::vector<int>(n, 0));
int totalSquares = 0;
for (int i = 0; i < m; ++i)
{
for (int j = 0; j < n; ++j)
{
if (matrix[i][j] == 1)
{
if (i == 0 || j == 0)
{
dp[i][j] = 1;
}
else
{
dp[i][j] = std::min({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]}) + 1;
}
totalSquares += dp[i][j];
}
}
}
return totalSquares;
}
};