Maximal Rectangle
https://leetcode.com/problems/maximal-rectangle/
具体算法请参照 largest rectangle in histrogram
code
class Solution {
public:
int maximalRectangle(vector<vector<char>>& matrix) {
int m = matrix.size();
if(m == 0)
{
return 0;
}
int n = matrix[0].size();
if(n == 0)
{
return 0;
}
vector<vector<int>> dp(m, vector<int>(n, 0));
for(int i = 0; i<m; i++)
{
for(int j = 0; j<n; j++)
{
if(matrix[i][j] == '1')
{
if(i == 0)
{
dp[i][j] = 1;
}
else
{
dp[i][j] = dp[i-1][j] + 1;
}
}
}
}
int max_area = 0;
for(int i = 0; i<m; i++)
{
dp[i].push_back(-1);
vector<int> stack;
for(int j = 0; j<n+1; j++)
{
while(!stack.empty() && dp[i][j] <= dp[i][stack.back()])
{
int h = dp[i][stack.back()];
stack.pop_back();
int w = stack.empty()?j:j-stack.back()-1;
max_area = max(max_area, h*w);
}
stack.push_back(j);
}
}
return max_area;
}
};