-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximalRectangle.cpp
More file actions
40 lines (40 loc) · 1.09 KB
/
Copy pathMaximalRectangle.cpp
File metadata and controls
40 lines (40 loc) · 1.09 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
class Solution {
public:
int maximalRectangle(vector<vector<char>>& matrix) {
int row_num = matrix.size();
const int col_num = row_num == 0 ? 0 : matrix[0].size();
vector<vector<int>> height(row_num, vector<int>(col_num));
for(int i = 0; i < row_num; i++)
{
for(int j = 0; j < col_num; j++)
{
if(matrix[i][j] == '0') height[i][j] = 0;
else height[i][j] = i == 0 ? 1 : height[i - 1][j] + 1;
}
}
int maxArea = 0;
for(int i = 0; i < row_num; i++)
{
maxArea = max(maxArea, largestRectangleArea(height[i]));
}
return maxArea;
}
private:
int largestRectangleArea(vector<int>& heights) {
int i = 0;
int maxArea = 0;
stack<int> s;
heights.push_back(0);
while (i < heights.size())
{
if (s.empty() || heights[s.top()] <= heights[i]) s.push(i++);
else
{
int k = s.top();
s.pop();
maxArea = max(maxArea, heights[k] * (s.empty() ? i : i - s.top() - 1));
}
}
return maxArea;
}
};