-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode_221_Maximal_Square.cpp
More file actions
44 lines (35 loc) · 1.38 KB
/
LeetCode_221_Maximal_Square.cpp
File metadata and controls
44 lines (35 loc) · 1.38 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
/*
221. Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square
containing only 1's and return its area.
Example:
Input:
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
Output: 4
*/
/*
We initialize another matrix (dp) with the same dimensions as the original one initialized with all 0’s.
dp(i,j) represents the side length of the maximum square whose bottom right corner is the cell with index (i,j) in the original matrix.
Starting from index (0,0), for every 1 found in the original matrix, we update the value of the current element as
dp(i,j)=min(dp(i−1,j),dp(i−1,j−1),dp(i,j−1))+1.
We also remember the size of the largest square found so far. In this way, we traverse the original matrix once and find out the required maximum size. This gives the side length of the square (say maxsqlen ). The required result is the area maxsqlen^2.
*/
int maximalSquare( vector<vector<char>>& matrix ) {
int m = matrix.size();
if( m == 0 )
return 0;
int n = matrix[0].size();
int maxSquareLen = 0;
vector<vector<int>> dp(m+1, vector<int>(n+1,0));
for( int i=1; i <= m; i++ ) {
for( int j=1; j <= n; j++ ) {
if( matrix[i-1][j-1] == '1')
dp[i][j] = min( min( dp[i-1][j], dp[i][j-1]), dp[i-1][j-1] ) + 1;
if( dp[i][j] > maxSquareLen ) maxSquareLen = dp[i][j];
}
}
return maxSquareLen*maxSquareLen;
}