-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0221-maximal-square.cpp
33 lines (28 loc) · 1023 Bytes
/
0221-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
/*
Given an m x n binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.
Ex. Input: matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
Output: 4
Time : O(m*n)
Space : O(m*n)
*/
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
int rows = matrix.size(), cols = matrix[0].size();
vector<vector<int>> dp (rows+1, vector<int>(cols+1, 0));
int maxi = 0;
for(int i = rows-1 ; i >= 0; --i) {
for(int j = cols-1 ; j >=0 ; --j) {
if(matrix[i][j] == '1') {
int right = dp[i][j+1], dia = dp[i+1][j+1], bottom = dp[i+1][j];
dp[i][j] = 1 + min(right, min(dia, bottom));
maxi = max(maxi, dp[i][j]);
}
else {
dp[i][j] = 0;
}
}
}
return maxi*maxi;
}
};