-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path221. Maximal Square.cpp
More file actions
111 lines (98 loc) · 3.59 KB
/
221. Maximal Square.cpp
File metadata and controls
111 lines (98 loc) · 3.59 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//Approach #1 Brute Force [Accepted]
//Runtime: 24 ms, faster than 33.17% of C++ online submissions for Maximal Square.
//Memory Usage: 8.2 MB, less than 100.00% of C++ online submissions for Maximal Square.
//time: O((mn)^2), space: O(1)
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
int m = matrix.size();
if(m == 0) return 0;
int n = matrix[0].size();
int maxslen = 0;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(matrix[i][j] == '0') continue;
int slen = 1;
bool valid = true;
while(slen + i < m && slen + j < n && valid){
for(int k = j; k <= j + slen; k++){
//i+slen: examine the (possible) last row of the square
if(matrix[i+slen][k] != '1'){
valid = false;
break;
}
}
for(int k = i; k <= i + slen; k++){
//j+slen: examine the (possible) last col of the square
if(matrix[k][j+slen] != '1'){
valid = false;
break;
}
}
if(valid){
slen++;
}
}
maxslen = max(maxslen, slen);
}
}
return maxslen * maxslen;
}
};
//Approach #2 (Dynamic Programming) [Accepted]
//Runtime: 24 ms, faster than 33.17% of C++ online submissions for Maximal Square.
//Memory Usage: 8.7 MB, less than 100.00% of C++ online submissions for Maximal Square.
//time: O(mn), space: O(mn)
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
int m = matrix.size();
if(m == 0) return 0;
int n = matrix[0].size();
int maxslen = 0;
vector<vector<int>> dp(m+1, vector(n+1, 0));
for(int i = 1; i <= m; i++){
for(int j = 1; j <= n; j++){
if(matrix[i-1][j-1] == '0') continue;
//check top, left and top-left
dp[i][j] = min({dp[i-1][j], dp[i][j-1], dp[i-1][j-1]}) + 1;
maxslen = max(maxslen, dp[i][j]);
}
}
return maxslen * maxslen;
}
};
//DP, O(n) space
//Runtime: 20 ms, faster than 77.64% of C++ online submissions for Maximal Square.
//Memory Usage: 8.6 MB, less than 100.00% of C++ online submissions for Maximal Square.
//time: O(mn), space: O(n)
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
int m = matrix.size();
if(m == 0) return 0;
int n = matrix[0].size();
int maxslen = 0;
vector<int> dp(n+1, 0);
int prev = 0; //top-left
for(int i = 1; i <= m; i++){
for(int j = 1; j <= n; j++){
/*
dp[j] of previous row
it will be used as 'prev' in next j,
at that time, it means the dp value of top-left corner
*/
int tmp = dp[j];
if(matrix[i-1][j-1] == '0'){
dp[j] = 0;
}else{
//check top, left and top-left
dp[j] = min({dp[j], dp[j-1], prev}) + 1;
maxslen = max(maxslen, dp[j]);
}
prev = tmp;
}
}
return maxslen * maxslen;
}
};