forked from iphkwan/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maximal_Rectangle.cc
75 lines (72 loc) · 2.32 KB
/
Maximal_Rectangle.cc
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
class Solution {
public:
int maximalRectangle(vector<vector<char> > &matrix) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (matrix.size() == 0) {
return 0;
}
vector<vector<int> > dp;
vector<int> tmp;
for (int i = 0; i < matrix[0].size(); i++) {
tmp.push_back(0);
}
for (int i = 0; i < matrix.size(); i++) {
dp.push_back(tmp);
if (matrix[i][0] == '1') {
dp[i][0] = 1;
}
for (int j = 1; j < matrix[i].size(); j++) {
if (matrix[i][j] == '1') {
dp[i][j] = dp[i][j - 1] + 1;
}
}
}
int ans = 0;
for (int i = 0; i < matrix.size(); i++) {
for (int j = 0; j < matrix[i].size(); j++) {
int width = matrix[i].size();
for (int k = i; k >= 0; k--) {
if (dp[k][j] == 0) {
break;
}
width = min(width, dp[k][j]);
ans = max(ans, width * (i - k + 1));
}
}
}
return ans;
}
};
//O(n^2) time solution
class Solution {
public:
int maximalRectangle(vector<vector<char> > &matrix) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int ret = 0;
int row = matrix.size();
if (row == 0) return ret;
int col = matrix[0].size();
vector<int> dp(col, 0);
stack<int> st;
int tmp;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
dp[j] = (matrix[i][j] == '1') ? (dp[j] + 1) : 0;
while (!st.empty() && dp[j] < dp[ st.top() ]) {
tmp = st.top();
st.pop();
ret = max(ret, dp[tmp] * ((st.empty() ? j : (j - 1 - st.top()))));
}
st.push(j);
}
while (!st.empty()) {
tmp = st.top();
st.pop();
ret = max(ret, dp[tmp] * ((st.empty() ? col : (col - 1 - st.top()))));
}
}
return ret;
}
};