-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxSquare.cpp
More file actions
64 lines (59 loc) · 1.88 KB
/
MaxSquare.cpp
File metadata and controls
64 lines (59 loc) · 1.88 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
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
int m = matrix.size(), n = matrix[0].size();
if (m == 1 || n == 1) {
for (auto v: matrix) {
for (auto c: v) {
if (c == '1') return 1;
}
}
return 0;
}
vector <vector <int>> dp(2, vector<int>(n, 0));
int Max = 0, idx = 1;
for (int j = 0; j < n; j++) {
if (matrix[0][j] == '1') {
dp[0][j] = 1;
Max = 1;
}
}
for (int i = 1; i < m; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == '1') {
dp[idx][j] = std::min({ dp[1- idx][j], j? dp[idx][j - 1]: 0, j? dp[1 - idx][j - 1]: 0 }) + 1;
Max = std::max(dp[idx][j], Max);
}
else dp[idx][j] = 0;
}
idx = 1 - idx;
}
return Max * Max;
}
int maximalSquare(vector<vector<char>>& matrix) {
int m = matrix.size(), n = matrix[0].size();
if (m == 1 || n == 1) {
for (auto v: matrix) {
for (auto c: v) {
if (c == '1') return 1;
}
}
return 0;
}
vector <int> dp(n + 1, 0);
int Max = 0, northwest = 0;
for (auto v: matrix) {
northwest = 0;
for (int col = 0; col < n; col++) {
int nextNorthwest = dp[col + 1];
if (v[col] == '1') {
dp[col + 1] = std::min({ dp[col], dp[col + 1], northwest }) + 1;
Max = std::max(dp[col + 1], Max);
}
else dp[col + 1] = 0;
northwest = nextNorthwest;
}
}
return Max * Max;
}
};