forked from hongtaocai/code_interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximalRectangle.cpp
More file actions
46 lines (45 loc) · 1.46 KB
/
MaximalRectangle.cpp
File metadata and controls
46 lines (45 loc) · 1.46 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
class Solution {
public:
int maximalRectangle(vector<vector<char> > &matrix) {
int m = matrix.size();
if(m==0) return 0;
int n = matrix[0].size();
if(n==0) return 0;
vector<int> height(n, 0);
int maxArea = 0;
for(int i=0;i<m;i++) {
for(int j=0;j<n;j++) {
height[j] = (matrix[i][j]=='0')? 0:height[j]+1;
}
maxArea = max(maxArea, largestRectangleArea(height));
}
return maxArea;
}
int largestRectangleArea(vector<int> &height) {
int maxArea = 0;
stack<int> heights;
stack<int> indexes;
for(int i=0;i<(int)height.size();i++) {
if(heights.empty() || heights.top() < height[i]) {
heights.push(height[i]);
indexes.push(i);
} else {
int recStart = -1;
while(!heights.empty() && heights.top() >= height[i]) {
maxArea = max(maxArea, heights.top()*(i-indexes.top()));
recStart = indexes.top();
heights.pop();
indexes.pop();
}
heights.push(height[i]);
indexes.push(recStart);
}
}
while(!heights.empty()) {
maxArea = max(maxArea, heights.top()*((int)height.size()-indexes.top()));
heights.pop();
indexes.pop();
}
return maxArea;
}
};