forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximalRectangle.cpp
134 lines (109 loc) · 3.19 KB
/
maximalRectangle.cpp
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
130
131
// Source : https://oj.leetcode.com/problems/maximal-rectangle/
// Author : Hao Chen
// Date : 2014-07-21
/**********************************************************************************
*
* Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle
* containing all ones and return its area.
*
**********************************************************************************/
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <vector>
using namespace std;
// The problem can be convert to the problem - "Largest Rectangle in Histogram"
// 1) we can take each row to calculate each row's histogram.
// 2) using the algorithm of "Largest Rectangle in Histogram" to find the largest area histogram.
// 3) tracking the maximal area.
//
// For the 1), it's easy.
// heights[i][j] = 1, if (i==0)
// heights[i][j] = heights[i-1][j] + 1;, if (i>0)
//
// For the 2), please referr to "Largest Rectangle in Histogram"
//
int largestRectangleArea(vector<int> &height) {
if (height.size()<=0) return 0;
height.push_back(0);
vector<int> stack;
int maxArea=0;
for(int i=0; i<height.size(); ){
if (stack.size()==0 || height[i] >= height[ stack.back() ] ){
stack.push_back(i);
i++;
}else{
int topIdx = stack.back();
stack.pop_back();
int area = height[topIdx] * ( stack.size()==0 ? i : i - stack.back() - 1 );
if (area > maxArea){
maxArea = area;
}
}
}
return maxArea;
}
int maximalRectangle(vector<vector<char> > &matrix) {
if (matrix.size()<=0 || matrix[0].size()<=0) return 0;
int row = matrix.size();
int col = matrix[0].size();
vector< vector<int> > heights(row, vector<int>(col));
int maxArea = 0;
for(int i=0; i<row; i++){
for(int j=0; j<col; j++) {
if (matrix[i][j]=='1'){
heights[i][j] = (i==0 ? 1 : heights[i-1][j] + 1);
}
}
int area = largestRectangleArea(heights[i]);
if (area > maxArea){
maxArea = area;
}
}
return maxArea;
}
void printArray(vector<int> &v)
{
cout << "{";
for(int i=0; i<v.size(); i++) {
cout << " " << v[i];
}
cout << "}" << endl;
}
void test(int a[], int n)
{
vector<int> v(a, a + n);
printArray(v);
cout << largestRectangleArea(v) << endl;
}
int main(int argc, char** argv)
{
#define TEST(a) test(a, sizeof(a)/sizeof(int))
int a0[] = {2,1,3,1};
TEST(a0);
int a1[] = {2,1,5,6,2,3};
TEST(a1);
cout << "------------------" << endl;
srand(time(0));
int r = 3;
int c = 3;
if (argc>2){
r = atoi(argv[1]);
c = atoi(argv[2]);
}
vector< vector<char> > matrix;
for(int i=0; i<r; i++){
vector<char> v;
cout << "[";
for(int j=0; j<c; j++) {
char ch = random()%2+'0';
v.push_back(ch);
cout << " " << ch;
}
matrix.push_back(v);
cout << " ]" << endl;
}
cout << "------------------" << endl;
cout << "maxArea = " << maximalRectangle(matrix) << endl;
return 0;
}