-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximal_Square.cpp
More file actions
39 lines (38 loc) · 1.04 KB
/
Maximal_Square.cpp
File metadata and controls
39 lines (38 loc) · 1.04 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
# number : 221
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
int length = 0;
int rows = (int)matrix.size();
if (rows <= 0)
return 0;
int cols = (int)matrix[0].size();
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
{
matrix[i][j] = matrix[i][j] - '0';
}
for (int i = 1; i < rows; i++)
for (int j = 1; j < cols; j++)
{
if (matrix[i][j] != 0)
{
matrix[i][j] = 1 + Min(matrix[i-1][j], matrix[i][j-1], matrix[i-1][j-1]);
}
}
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
{
if (matrix[i][j] > length)
length = matrix[i][j];
}
return length * length;
}
int Min(int a, int b, int c)
{
if (a < b)
return a < c ? a : c;
else
return b < c ? b : c;
}
};