-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximal_Rectangle.cpp
More file actions
155 lines (134 loc) · 2.95 KB
/
Maximal_Rectangle.cpp
File metadata and controls
155 lines (134 loc) · 2.95 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Source : https://oj.leetcode.com/problems/maximal-rectangle/
// Author : zheng yi xiong
// Date : 2015-01-06
/**********************************************************************************
*
* Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
*
**********************************************************************************/
#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <vector>
using namespace std;
class Solution {
public:
int maximalRectangle(vector<vector<char> > &matrix) {
if (matrix.empty() || matrix[0].empty())
{
return 0;
}
int row_sum = matrix.size();
int col_sum = matrix[0].size();
vector<int> col_hight(col_sum, 0);
vector<int> hight_len(row_sum + 1, 0);
int *pHight = new int[row_sum + 1];
for (int row = 0; row < row_sum; ++row)
{
memset(pHight, 0, sizeof(int) * (row_sum + 1));
if ('1' == matrix[row][0])
{
col_hight[0] += 1;
pHight[col_hight[0]] = 1;
for (int i = 1; i < col_hight[0]; ++i)
{
pHight[i] += 1;
}
}
else
{
col_hight[0] = 0;
}
for (int col = 1; col < col_sum; ++col)
{
if ('1' == matrix[row][col])
{
col_hight[col] += 1;
pHight[col_hight[col]] += 1;
if (col_hight[col - 1] > col_hight[col])
{
for (int i = col_hight[col] + 1; i <= col_hight[col - 1]; ++i)
{
if (hight_len[i] < pHight[i])
{
hight_len[i] = pHight[i];
}
pHight[i] = 0;
}
}
for (int i = 1; i < col_hight[col]; ++i)
{
pHight[i] += 1;
}
}
else
{
col_hight[col] = 0;
for (int i = 1; i <= row + 1; ++i)
{
if (hight_len[i] < pHight[i])
{
hight_len[i] = pHight[i];
}
}
memset(pHight, 0, sizeof(int) * (row_sum + 1));
}
}
for (int i = 1; i <= col_hight[col_sum - 1]; ++i)
{
if (hight_len[i] < pHight[i])
{
hight_len[i] = pHight[i];
}
}
}
delete []pHight;
int max_area = 0, temp_area = 0;
for (int row = 0; row <= row_sum; ++row)
{
temp_area = hight_len[row] * row;
if (temp_area > max_area)
{
max_area = temp_area;
}
}
return max_area;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
int row = 5;
int col = 5;
int ones = 5;
if ( argc > 3) {
row = _wtoi(argv[1]);
col = _wtoi(argv[2]);
ones = _wtoi(argv[3]);
}
vector<vector<char> > matrix(row, vector<char>(col, '1'));
matrix[0][3] = '0';
matrix[3][0] = '0';
matrix[3][1] = '0';
matrix[3][2] = '0';
matrix[4][4] = '0';
/*srand(time(0));
for(int i = 0; i< ones; ++i) {
matrix[rand() % row][rand() % col] = '0';
}*/
cout<<"2D binary matrix:\n";
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
{
cout<<' '<<matrix[i][j];
}
cout<<endl;
}
cout<<endl;
//system("pause");
Solution so;
int area = so.maximalRectangle(matrix);
cout<<"area = "<<area<<endl;
system("pause");
return 0;
}