首页 > 其他 > 详细

Maximal Rectangle

时间:2015-01-12 16:34:51      阅读:254      评论:0      收藏:0      [点我收藏+]

求一个01二位数组最大的矩阵,把它化为直角图再一行一行地算

class Solution{
public:
  int maximalRectangle(vector<vector<char> > &martix){
    if(martix.size() == 0 || martix[0].size() == 0) return 0;
    int height[1000][1000];
    int maxx = -1;
    int row = martix.size();
    int col = martix[0].size();
    for(int j =0; j < col; j++)
      for(int i = 0; i < row; i++){
        if(martix[i][j] == '0') height[i][j] = 0;
        else if(i == 0) height[0][j] = 1;
        else height[i][j] = height[i-1][j]+1;
      }
    stack<int>s;
    for(int i = 0; i < row; ++i){
      for(int j = 0; j < col; ++j){
        if(s.empty()) s.push(j);
        else {
          while(!s.empty() && height[i][s.top()] > height[i][j]){
            int tmp = s.top();
            s.pop();
              maxx = max(maxx,(s.empty()?j:(j-s.top()-1))*height[i][tmp]);
          }
          s.push(j);
        }
      }
      while(!s.empty()){
        int tmp = s.top();
        s.pop();
              maxx = max(maxx,(s.empty()?col:(col-s.top()-1))*height[i][tmp]);
      }
    }
    return maxx;
  }
};


Maximal Rectangle

原文:http://blog.csdn.net/sina012345/article/details/42643227

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!