Skip to content

Commit 4f8d444

Browse files
committed
#335 fixed issues
1 parent 7246c0f commit 4f8d444

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

Dynamic Programming/Maximal_square.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@ using namespace std;
3939
int maximalSquare(vector<vector<char>>& matrix) {
4040
int n = matrix.size();
4141
int m = matrix[0].size();
42-
vector<vector<int>>dp(n, vector<int>(m, 0));
43-
dp[n-1][m-1] = matrix[n-1][m-1]-'0';
42+
vector<vector<int>>dp(n, vector<int>(m, 0)); // dp matrix
43+
dp[n-1][m-1] = matrix[n-1][m-1]-'0'; // dp[n-][m-1] is same as matrix[n-1][m-1]
4444
int ans = dp[n-1][m-1];
45+
//last column is 1 or 0
4546
for(int i=0;i<n-1;i++){
4647
dp[i][m-1]=matrix[i][m-1]-'0';
4748
ans = max(ans, dp[i][m-1]);
48-
}
49+
}
50+
//last row is 1 or 0
4951
for(int j=0;j<m-1;j++){
5052
dp[n-1][j]=matrix[n-1][j]-'0';
5153
ans = max(ans, dp[n-1][j]);
@@ -54,10 +56,13 @@ int maximalSquare(vector<vector<char>>& matrix) {
5456
{
5557
for(int j=m-2;j>=0;j--)
5658
{
59+
// dp[i][j] is 0 when matrix[i][j] is 0
5760
if(matrix[i][j]=='0')
58-
dp[i][j]=0;
61+
dp[i][j]=0;
62+
//dp[i][j] is 1 + min(right_hand, diagonally_down, down)
5963
else
6064
dp[i][j] = 1+min(dp[i][j+1],min(dp[i+1][j],dp[i+1][j+1]));
65+
// ans is max of all values in dp
6166
ans = max(dp[i][j], ans);
6267
}
6368
}

0 commit comments

Comments
 (0)