现在的位置: 首页 > 综合 > 正文

Maximal Rectangle

2018年04月08日 ⁄ 综合 ⁄ 共 715字 ⁄ 字号 评论关闭

求一个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;
  }
};
【上篇】
【下篇】

抱歉!评论已关闭.