原文地址:http://blog.csdn.net/littlestream9527/article/details/19641013
方法二的参考地址:http://hi.baidu.com/mzry1992/item/030f9740e0475ef7dc0f6cba
隔了一个月又回来了。这题之前做过,思路就是根据上一题中求直方图中的最大矩形面积来求全部包含1的最大矩形面积,就是多加了两层循环。
- int largestRectangleArea3(vector<int> &height) {
- stack<int> s;
- int len=height.size(),maxx=0;
- for(int i=0;i<len;++i)
- {
- if(s.empty())s.push(i);
- else
- {
- while(!s.empty()&&height[s.top()]>height[i])
- {
- int ph=s.top();
- s.pop();
- if(!s.empty())
- maxx=max(maxx,(i-s.top()-1)*height[ph]);
- else
- maxx=max(maxx,i*height[ph]);
- }
- s.push(i);
- }
- }
- while(!s.empty())
- {
- int ph=s.top();
- s.pop();
- if(!s.empty())
- maxx=max(maxx,(len-s.top()-1)*height[ph]);
- else
- maxx=max(maxx,len*height[ph]);
- }
- return maxx;
- }
- int maximalRectangle(vector<vector<char> > &matrix) {
- if (matrix.empty()) {
- return 0;
- }
- int lenRow = matrix.size();
- if(lenRow==0)
- return 0;
- int lenColumn = matrix[0].size();
- vector<int> pcolumn;
- for (int i=0;i<lenColumn;i++)
- pcolumn.push_back(0);
- int tempmax = 0,maxx = 0;
- for (int i=0;i<lenRow;i++)
- {
- for (int k=0;k<lenColumn;k++)
- {
- pcolumn[k] = 0;
- for (int j=i;j>=0&&matrix[j][k]=='1';j--)
- {
- pcolumn[k]++;
- }
- }
- tempmax = largestRectangleArea3(pcolumn);
- if(tempmax>maxx)
- maxx = tempmax;
- }
- return maxx;
- }
- //
- int _tmain(int argc, _TCHAR* argv[])
- {
- vector<vector<char> >matrix;
- vector<char> height;
- height.push_back('0');
- height.push_back('0');
- height.push_back('0');
- height.push_back('0');
- height.push_back('0');
- matrix.push_back(height);
- height.clear();
- height.push_back('0');
- height.push_back('1');
- height.push_back('0');
- height.push_back('1');
- height.push_back('0');
- matrix.push_back(height);
- height.clear();
- height.push_back('0');
- height.push_back('0');
- height.push_back('0');
- height.push_back('0');
- height.push_back('0');
- matrix.push_back(height);
- height.clear();
- height.push_back('0');
- height.push_back('1');
- height.push_back('1');
- height.push_back('0');
- height.push_back('0');
- matrix.push_back(height);
- height.clear();
- height.push_back('0');
- height.push_back('1');
- height.push_back('0');
- height.push_back('0');
- height.push_back('0');
- matrix.push_back(height);
- height.clear();
- cout<<maximalRectangle(matrix)<<endl;
- system("pause");
- return 0;
- }
- int maximalRectangle01(vector<vector<char> > &matrix) {
- if (matrix.empty()) {
- return 0;
- }
- int n = matrix[0].size();
- vector<int> H(n);
- vector<int> L(n);
- vector<int> R(n, n);
- int ret = 0;
- for (int i = 0; i < matrix.size(); ++i) {
- int left = 0, right = n;
- // calculate L(i, j) from left to right
- for (int j = 0; j < n; ++j) {
- if (matrix[i][j] == '1') {
- ++H[j];
- L[j] = max(L[j], left);
- }
- else {
- left = j+1;
- H[j] = 0; L[j] = 0; R[j] = n;
- }
- }
- // calculate R(i, j) from right to right
- for (int j = n-1; j >= 0; --j) {
- if (matrix[i][j] == '1') {
- R[j] = min(R[j], right);
- ret = max(ret, H[j]*(R[j]-L[j]));
- }
- else {
- right = j;
- }
- }
- }
- return ret;
- }
自己试着写了这种方法的程序,写出来了,是错的,对于下面的测试用例不好使。这题太难理解了。。。也说不明白。。。
把上面的程序用测试用例1011,0101,1110调试了几次,就是想看懂程序:当matrix【row】【i】不为零时,假设L[i]=kl,R[i]=kr,H[i]=kh,说明要构成的最大矩形(全为1)高为kh,左边界位置为kl,右边界位置为kr。最关键的是L[i]的求法利用了left变量递推,每次遇到当前值为0就更新left,也就更新了之后的L[i]。R[i]和right是同理的。这题更微妙的是当前值为0时H[i]为0是必须的,因为当对应的下个位置为1会加一。这题L[i]=max(L[i],left);也就是说当left>L[i]时更新L[i],否则当前值得L[i]就是上一行的L[i]。
真没理解透,跑程序知道是正确的,但不知道为什么这样是正确的。。。。。。。。。。。。
再回首:left的作用就是记录当前行的左起第一个为1的位置,上一行的存在L[i]里,所以L[i]=max(L[i],left)才正确,说的就是当此行的left变大了,那么此行的L[i]也应该变大,此行的L[i]对应的高度是此行的H[i](即H[i]已经加一了,因为当前的元素肯定不为0)。