-
Notifications
You must be signed in to change notification settings - Fork 481
/
0085.cpp
34 lines (32 loc) · 937 Bytes
/
0085.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
#include <vector>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
int maximalRectangle(vector<vector<char>>& matrix)
{
if (matrix.empty()) return 0;
int r = matrix.size(), c = matrix[0].size();
vector<int> heights(c+1, 0);
int res = 0;
for (auto& row : matrix)
{
for (int i = 0; i < c; ++i)
{
heights[i] = row[i] == '1' ? heights[i] + 1 : 0;
}
vector<int> s{-1};
for (int idx = 0; idx < c+1; ++idx)
{
while (heights[idx] < heights[s.back()])
{
int h = heights[s.back()]; s.pop_back();
res = max(res, h*(idx - s.back() - 1));
}
s.push_back(idx);
}
}
return res;
}
};