-
Notifications
You must be signed in to change notification settings - Fork 979
/
solution.h
32 lines (31 loc) · 1.06 KB
/
solution.h
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
#include <vector>
using std::vector;
#include <stack>
using std::stack;
#include <algorithm>
using std::max; using std::min;
class Solution {
public:
int largestRectangleArea(vector<int> &height) {
int max_area = 0, i = 0, size = height.size();
for (stack<int> stk; i<size || !stk.empty(); )
if (stk.empty() || (i != size && height[stk.top()] <= height[i])) stk.push(i++);
else {
int tp = stk.top(); stk.pop();
max_area = max(max_area, height[tp] * (stk.empty() ? i : i-stk.top()-1));
}
return max_area;
}
int maximalRectangle(vector<vector<char> > &matrix) {
if (matrix.empty()) return 0;
int max_area = 0;
vector<int> height(matrix[0].size(), 0);
for (size_t i=0; i<matrix.size(); ++i) {
for (size_t j=0; j<matrix[0].size(); ++j)
if (matrix[i][j] == '0') height[j] = 0;
else ++height[j];
max_area = max(max_area, largestRectangleArea(height));
}
return max_area;
}
};