Skip to content

Commit 8bbf8c2

Browse files
authored
Maximal Square
1 parent a3830dd commit 8bbf8c2

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Dynamic-Programming/maximalSquare.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int maximalSquare(vector<vector<char>>& matrix) {
4+
int r = matrix.size();
5+
if (r ==0) return 0;
6+
int c = matrix[0].size();
7+
vector<vector<int>> dp(r+1, vector<int>(c+1, 0));
8+
int maxsq = 0;
9+
for(int i = 1; i <= r; ++i) {
10+
for(int j = 1; j <= c; ++j) {
11+
if(matrix[i-1][j-1] =='1'){
12+
dp[i][j] = min(dp[i-1][j], min(dp[i-1][j-1], dp[i][j-1]))+1;
13+
maxsq = max(maxsq, dp[i][j]);
14+
}
15+
}
16+
}
17+
return maxsq*maxsq;
18+
}
19+
20+
};

0 commit comments

Comments
 (0)