-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaily132.cpp
More file actions
65 lines (54 loc) · 1.95 KB
/
Copy pathdaily132.cpp
File metadata and controls
65 lines (54 loc) · 1.95 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Solution 1 - FAIL
class Solution {
public:
int countSquares(vector<vector<int>>& matrix) {
/*
prefix sum to figure out how many 1s up to ith row and column
check thru all possible squares to find squares with a one in it
rows VV
cols ->>
*/
auto one_rows = std::vector<int>(matrix.size(), 0);
auto one_cols = std::vector<int>(matrix[0].size(), 0);
for (auto i = 0; i < matrix.size(); ++i) {
for (auto j = 0; j < matrix[0].size(); ++j) {
if (j == 0)
one_rows[i] = matrix[i][j] == 1 ? 1 : 0;
else
one_rows[i] = matrix[i][j] == 1 ? one_rows[i - 1] + 1 : one_rows[i - 1];
if (i == 0)
one_cols[j] = matrix[i][j] == 1 ? 1 : 0;
else
one_cols[j] = matrix[i][j] == 1 ? one_cols[j - 1] + 1 : one_cols[j - 1];
}
}
int squares = 0;
for (int i = one_rows.size() - 1; i >= 0; --i) {
for (int j = one_cols.size() - 1; j >= 0; --j) {
if (one_rows[i] * one_cols[j] >= j * j)
squares++;
}
}
return squares;
}
}
// Solution 2
class Solution {
public:
int countSquares(vector<vector<int>>& matrix) {
int rows = matrix.size();
int cols = matrix[0].size();
int squares = 0;
// Use matrix itself as the DP table
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if (matrix[i][j] == 1 && i > 0 && j > 0) {
// Update the cell to store the size of the largest square ending at (i, j)
matrix[i][j] = min({matrix[i - 1][j], matrix[i][j - 1], matrix[i - 1][j - 1]}) + 1;
}
squares += matrix[i][j]; // Sum up all squares sizes
}
}
return squares;
}
};