File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 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+ };
You can’t perform that action at this time.
0 commit comments