-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMaximal Rectangle
More file actions
66 lines (59 loc) · 1.91 KB
/
Maximal Rectangle
File metadata and controls
66 lines (59 loc) · 1.91 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
class Solution {
public:
int maximalRectangle(vector<vector<char>>& matrix) {
int m = matrix.size();
if(m==0) return 0;
int n = matrix[0].size();
vector<int> height(n);
int max_rectangle_area = INT_MIN;
for(int i = 0;i<m;i++)
{
for(int j = 0;j<n;j++)
{
if(matrix[i][j] =='0') height[j] = 0;
else height[j] = height[j]+1;
}
int rectangle_area = largestRectangleArea(height);
if(rectangle_area>max_rectangle_area)
max_rectangle_area = rectangle_area;
}
return max_rectangle_area;
}
int largestRectangleArea(vector<int>& height) {
stack<int> index; //using the stack to save the indexes.
int area, max_area = INT_MIN;
int n = height.size();
if(n==0) return 0;
int i;
for(i = 0;i<n;i++)
{
int curr_height = height[i];
if(index.empty() || (!index.empty() && curr_height >=height[index.top()]))
{
index.push(i);
}
else
{
while(!index.empty())
{
int top_index = index.top();
if(curr_height>height[top_index]) break;
index.pop();
if(index.empty()) area = height[top_index] * i;
else area = height[top_index]*(i-index.top()-1);
if(area>max_area) max_area = area;
}
index.push(i);
}
}
while(!index.empty())
{
int top_index = index.top();
index.pop();
if(index.empty()) area = height[top_index] * i;
else area = height[top_index]*(i-index.top()-1);
if(area>max_area) max_area = area;
}
return max_area;
}
};