-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximalSquare.cc
71 lines (55 loc) · 1.6 KB
/
MaximalSquare.cc
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
65
66
67
68
69
#include "leet.h"
#include <algorithm>
#include <cstring>
class Solution{
public:
int maximalSquare(vector<vector<char> > &matrix) {
if (matrix.empty()) {
return 0;
}
int rows = matrix.size();
int cols = matrix[0].size();
int dp[rows + 1][cols + 1];
std::memset(dp, 0, sizeof dp);
int ret = 0;
for (int i = 0; i < rows; ++i) {
dp[i][0] = (matrix[i][0] == '0') ? 0 : 1;
ret = std::max(ret, dp[i][0]);
}
for (int i = 0; i < cols; ++i) {
dp[0][i] = (matrix[0][i] == '0') ? 0 : 1;
ret = std::max(ret, dp[0][i]);
}
for (int x = 1; x < rows; ++x) {
for (int y = 1; y < cols; ++y) {
if (matrix[x][y] == '1') {
dp[x][y] = 1 + std::min(dp[x - 1][y - 1], std::min(dp[x - 1][y], dp[x][y - 1]));
//cout << x << ", " << y << ": " << dp[x][y] << endl;
ret = std::max(ret, dp[x][y]);
}
}
}
return ret * ret;
}
};
int main(){
Solution slu;
vector<vector<char> > a {
{'1', '0', '1', '0', '0'},
{'1', '0', '1', '1', '1'},
{'1', '1', '1', '1', '1'},
{'1', '0', '0', '1', '0'},
};
vector<vector<char> > b {
{'0', '1'},
};
vector<vector<char> > c {
{'1','1','1','1'},
{'1','1','1','1'},
{'1','1','1','1'},
};
cout << slu.maximalSquare(a) << endl;
cout << slu.maximalSquare(b) << endl;
cout << slu.maximalSquare(c) << endl;
return 0;
}