-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0221-maximal-square.cpp
More file actions
45 lines (40 loc) · 1.28 KB
/
0221-maximal-square.cpp
File metadata and controls
45 lines (40 loc) · 1.28 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
#include<vector>
using namespace std;
class Solution {
public:
static int maximalSquare(vector<vector<char>> &matrix) {
int m = matrix.size();
int n = matrix[0].size();
vector<int> curr(n, 0);
int maxSq = 0;
for (int i = 0; i < n; i++)
maxSq = max(curr[i] = matrix[0][i] - '0', maxSq);
int prev, temp;
for (int i = 1; i < m; i++) {
prev = curr[0];
curr[0] = matrix[i][0] - '0';
maxSq = max(maxSq, curr[0]);
for (int j = 1; j < n; j++) {
temp = curr[j];
if (matrix[i][j] - '0') {
curr[j] = min(prev, min(curr[j], curr[j - 1])) + 1;
maxSq = max(maxSq, curr[j]);
} else {
curr[j] = 0;
}
prev = temp;
}
}
return maxSq * maxSq;
}
};
int main() {
vector<vector<char>> q{{'0', '0', '1', '0'},
{'1', '1', '1', '1'},
{'1', '1', '1', '1'},
{'1', '1', '1', '0'},
{'1', '1', '0', '0'},
{'1', '1', '1', '1'},
{'1', '1', '1', '0'}};
Solution::maximalSquare(q);
}