Skip to content

Commit 91d7167

Browse files
authored
Create 85. Maximal Rectangle (#456)
2 parents 1d1b090 + 4aefe30 commit 91d7167

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

85. Maximal Rectangle

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public:
3+
int maximalRectangle(vector<vector<char>>& matrix) {
4+
int maxArea = 0;
5+
6+
if(matrix.size() == 0 || matrix[0].size() == 0) return 0;
7+
int cols = matrix[0].size();
8+
int rows = matrix.size();
9+
vector<int>histogram(cols+1, 0);
10+
histogram[cols] = -1;
11+
stack<pair<int,int>>monotonic;
12+
13+
for(auto row: matrix) {
14+
for(int i = 0; i < cols; i++) {
15+
if(row[i] == '1') {
16+
histogram[i]++;
17+
} else {
18+
histogram[i] = 0;
19+
}
20+
}
21+
22+
for(int i = 0; i < cols+1; i++) {
23+
int x = 0;
24+
while(!monotonic.empty() && monotonic.top().first >= histogram[i]) {
25+
auto tp = monotonic.top(); monotonic.pop();
26+
int h = tp.first, steps = tp.second;
27+
x += steps;
28+
maxArea = max(maxArea, h*x);
29+
}
30+
monotonic.push({histogram[i], x+1});
31+
}
32+
}
33+
34+
return maxArea;
35+
}
36+
};

0 commit comments

Comments
 (0)