-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathsolution.cpp
More file actions
44 lines (40 loc) · 1.1 KB
/
Copy pathsolution.cpp
File metadata and controls
44 lines (40 loc) · 1.1 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
class Solution
{
public:
int largestRectangleArea(vector<int> &heights)
{
stack<int> st;
int maxArea = 0;
heights.push_back(0); // sentinel
for (int i = 0; i < heights.size(); i++)
{
while (!st.empty() && heights[st.top()] > heights[i])
{
int h = heights[st.top()];
st.pop();
int w = st.empty() ? i : i - st.top() - 1;
maxArea = max(maxArea, h * w);
}
st.push(i);
}
heights.pop_back();
return maxArea;
}
int maximalRectangle(vector<vector<char>> &matrix)
{
if (matrix.empty())
return 0;
int cols = matrix[0].size();
vector<int> heights(cols, 0);
int ans = 0;
for (auto &row : matrix)
{
for (int j = 0; j < cols; j++)
{
heights[j] = (row[j] == '1') ? heights[j] + 1 : 0;
}
ans = max(ans, largestRectangleArea(heights));
}
return ans;
}
};