#include "iostream"
#include "vector"
#include "stack"
using namespace std;
class Solution
{
public:
int maximalRectangle(vector<vector<char>> &matrix)
{
int max_y = matrix.size();
if (max_y == 0)
{
return 0;
}
int max_x = matrix[0].size();
int ans = 0, tmp;
vector<int> dp(max_x, 0);
for (int i = 0; i < max_y; i++)
{
for (int j = 0; j < max_x; j++)
{
dp[j] = matrix[i][j] == '1' ? dp[j] + 1 : 0;
}
tmp = largestRectangleArea(dp);
ans = ans > tmp ? ans : tmp;
}
return ans;
}
int largestRectangleArea(vector<int> &heights)
{
stack<int> z;
z.push(-1);
int n = heights.size();
int ans = 0, tmp;
for (int i = 0; i < n; ++i)
{
while (z.top() != -1 && heights[i] < heights[z.top()])
{
int tmp = z.top();
z.pop();
tmp = heights[tmp] * (i - 1 - z.top());
ans = ans > tmp ? ans : tmp;
}
z.push(i);
}
while (z.top() != -1)
{
int tmp = z.top();
z.pop();
tmp = heights[tmp] * (n - 1 - z.top());
ans = ans > tmp ? ans : tmp;
}
return ans;
}
};
int main(int argc, char const *argv[])
{
int y, x;
cin >> y;
cin >> x;
char c;
vector<vector<char>> matrix(y, vector<char>(x));
for(int i=0;i<y;i++)
{
for(int j=0;j<x;j++)
{
cin >> c;
matrix[i][j] = c;
}
}
Solution so;
cout << so.maximalRectangle(matrix);
return 0;
}
【Leetcode】85.最大矩形C++(柱状图栈法)
猜你喜欢
转载自blog.csdn.net/weixin_44936889/article/details/104100012
今日推荐
周排行