class Solution {
public:
int maximalRectangle(vector<vector<char> > &matrix) {
int m=matrix.size();
if (m==0) return 0;
int n=matrix[0].size();
int res=0;
vector<vector<int> > mat(m, vector<int> (n+1,0));
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++) {
if (matrix[i][j]=='1') {
if (i==0) mat[i][j]=1;
else mat[i][j]=mat[i-1][j]+1;
}
}
}
for (int i=0; i<m; i++) {
int j=0;
stack<int> s;
while (j<mat[i].size()) {
if (s.empty() || mat[i][j]>=mat[i][s.top()]) s.push(j++);
else {
int t=s.top();
s.pop();
res = max(res,mat[i][t]*(s.empty()?j:j-s.top()-1));
}
}
}
return res;
}
};Maximal Rectangle
最新推荐文章于 2021-06-14 17:39:06 发布
本文介绍了一种求解二维矩阵中最大全'1'矩形面积的算法实现。该算法通过构建辅助矩阵来记录每行连续'1'的数量,并利用单调栈遍历每行更新最大矩形面积。
918

被折叠的 条评论
为什么被折叠?



