forked from codemistic/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximal Square.cpp
44 lines (36 loc) · 1.12 KB
/
Maximal Square.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
36
37
38
39
40
41
42
43
44
// https://leetcode.com/problems/maximal-square/
// Maximal Square
class Solution {
public:
int solveTab(vector<vector<char>>& matrix, int &maxi)
{
vector<int>prev(matrix[0].size() + 1, 0);
vector<int>curr(matrix[0].size() + 1);
for (int row = matrix.size() - 1; row >= 0; row --)
{
for (int column = matrix[0].size() - 1; column >= 0; column--)
{
int right = curr[column + 1];
int diag = prev[column + 1];
int down = prev[column];
if (matrix[row][column] == '1')
{
int ans = 1 + min(right, min(diag, down));
maxi = max(maxi , ans);
curr[column] = ans;
}
else
{
curr[column] = 0;
}
}
prev = curr;
}
return 0;
}
int maximalSquare(vector<vector<char>>& matrix) {
int maxi = 0;
solveTab(matrix, maxi);
return maxi * maxi;
}
};