-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaximalRectangle.cc
131 lines (112 loc) · 2.95 KB
/
MaximalRectangle.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <vector>
#include <string>
#include <iostream>
using std::vector;
using std::string;
using std::cin;
using std::cout;
using std::endl;
#include <cstdlib>
using std::atoi;
struct ListNode{
int val;
ListNode *next;
ListNode(int x):val(x), next(nullptr){}
ListNode(std::initializer_list<int> vals):val(0), next(nullptr){
if(0 == vals.size()){
return;
}
auto p = vals.begin();
this->val = *p;
auto last = this;
for(++p; p != vals.end(); ++p){
last->next = new ListNode(*p);
last = last->next;
}
}
};
std::ostream& operator<<(std::ostream& os, ListNode *head){
for(; head; head = head->next){
os << head->val << " ";
}
return os;
}
#include <algorithm>
#include <cstring>
#include <stack>
using std::stack;
class Solution{
public:
int largestRectangleArea(vector<int> &height){
int sz = height.size();
stack<int> st;
int left[sz + 10], right[sz + 10];
for(int i = 0; i < sz; ++i){
while(!st.empty()){
if(height[st.top()] < height[i]){
left[i] = st.top();
st.push(i);
break;
}
st.pop();
}
if(st.empty()){
left[i] = -1;
st.push(i);
}
}
while(!st.empty())st.pop();
for(int i = sz-1; i >= 0; --i){
while(!st.empty()){
if(height[st.top()] < height[i]){
right[i] = st.top();
st.push(i);
break;
}
st.pop();
}
if(st.empty()){
right[i] = -1;
st.push(i);
}
}
int ans = 0;
for(int i = 0; i < sz; ++i){
int llen = (-1 == left[i])? i : (i - left[i] - 1);
int rlen = (-1 == right[i])? (sz - 1 - i) : (right[i] - i - 1);
ans = std::max(ans, (1 + llen + rlen) * height[i]);
}
return ans;
}
int maximalRectangle(vector<vector<char> > &matrix){
if(matrix.empty()){
return 0;
}
int n = matrix.size();
int m = matrix[0].size();
vector<int> height(m, 0);
int ans = 0;
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
if('0' == matrix[i][j]){
height[j] = 0;
}else{
height[j]++;
}
}
ans = std::max(ans, largestRectangleArea(height));
}
return ans;
}
};
int main(){
Solution slu;
vector<vector<char> > a = {
{'0','0','1','0','1','1','0','1'},
{'1','0','1','1','1','1','0','1'},
{'0','0','0','0','1','1','0','0'},
};
//vector<int> a = {1,1};
cout << slu.maximalRectangle(a) << endl;
return 0;
}