-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maximal_rectangle
59 lines (53 loc) · 1.89 KB
/
Maximal_rectangle
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
/*
Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
*/
class Solution {
public:
int largestRectangleArea(vector<int> &height) {
vector<int> leftBound(height.size(),-1);
vector<int> rightBound(height.size(),-1);
//first determine the leftBound
int i=0,maxArea=0;
while(i<height.size())
{
int j=i;//j is used to moving to left and track the index of left bound.
while(j>0 && height[i]<=height[j-1]) j=leftBound[j-1];//we use the previous result here
leftBound[i++]=j;
}
//secondly, we determine the rightBound
i=height.size()-1;
while(i>=0)
{
int j=i;
while(j<height.size()-1&&height[i]<=height[j+1]) j=rightBound[j+1];
rightBound[i--]=j;
}
//Now since we get the width(=rightBound-leftBound+1), we multiply it by the corresponding height
for (int i=0; i<height.size(); ++i)
maxArea=max(maxArea,height[i]*(rightBound[i]-leftBound[i]+1));
return maxArea;
}
int maximalRectangle(vector<vector<char> > &matrix) {
if (matrix.size()==0) return 0;
int maxArea=0, area=0;
//m and n are the size of the matrix
int nrow=matrix.size(), ncol=matrix[0].size();
//vector histo denote the histogram of the matrix from row 0 to row nrow-1.
vector<int> histo(ncol,0);
for (int i=0; i<nrow; ++i)
{
for (int j=0; j<ncol; ++j)
{
if (matrix[i][j]=='1') histo[j]++;
if (matrix[i][j]=='0') histo[j]=0;
}
area=largestRectangleArea(histo);
if (area>maxArea) maxArea=area;
}
return maxArea;
}
};
REMARK:
1. IDEA:The idea of this problem is based on the problem Largest Rectangle in Histogram. We transform this problem to the previous problem Largest Rectangle in Histogram. We use a one-dimensional vector to store the height of the histogram.
2. Time: O(n^2)