Leetcode, MaximalRectangle
暂时不懂,记录下答案
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int solution(vector<vector<char>> &mat)
{
const int m = mat.size();
const int n = mat[0].size();
vector<int> HeightVec(n, 0);
vector<int> LeftVec(n, 0);
vector<int> RightVec(n, n);
int ret = 0;
for( int i = 0; i < m; ++i )
{
int left = 0, right = n;
// calculate Left(i, j) from left to right
for ( int j = 0; j < n; ++j )
{
if ( mat[i][j] == '1' )
{
++HeightVec[j];
LeftVec[j] = std::max(LeftVec[j], left);
}
else
{
left = j + 1;
HeightVec[j] = 0; LeftVec[j] = 0, RightVec[j] = n;
}
}
// calculate Right(i, j) from right to left
for ( int j = n - 1; j >= 0; --j)
{
if ( mat[i][j] == '1' )
{
RightVec[j] = std::min(RightVec[j], right);
ret = std::max(ret, HeightVec[j] * (RightVec[j] - LeftVec[j]));
}
else
{
right = j;
}
}
}
return ret;
}
int main()
{
vector<vector<char>> mat = {
{'1', '0', '1', '0', '0'},
{'1', '0', '1', '1', '1'},
{'1', '1', '1', '1', '1'},
{'1', '0', '0', '1', '0'}
};
cout << solution(mat) << endl; //6
return 0;
}