leetcode85 最大矩形(hard)

題目

給定一個僅包含 0 和 1 的二維二進制矩陣,找出只包含 1 的最大矩形,並返回其面積。

示例:

輸入:
[
  ["1","0","1","0","0"],
  ["1","0","1","1","1"],
  ["1","1","1","1","1"],
  ["1","0","0","1","0"]
]
輸出: 6

 

思路1:單調棧

這題還是比較有技巧性的,可以將它轉換成,leetcode 84直方圖最大矩形(https://leetcode.com/problems/largest-rectangle-in-histogram/),對於每一行將其作爲以該行爲底的一個直方圖

這裏計算最大矩形可以用單調棧:

  1. 在高度數組(不是棧)最後面加上一個0,這樣原先的最後一個板子也可以被處理

  2.  0肯定比任何的heights都小,所以0放在heights最後,一定會計算最後一塊板到前面板子的面積

  3. 棧內存放下標,維護棧內是遞增的高度

class Solution {
public:
    int maximalRectangle(vector<vector<char>>& matrix) {
        if(matrix.empty() || matrix[0].empty()) return 0;
        int m = matrix.size(),n = matrix[0].size(),max_num = 0;
        vector<vector<int>> heights(m,vector<int>(n+1,0));
        for(int j = 0;j < n;j++){
            if(matrix[0][j] == '1'){
                heights[0][j] = 1;
            }
        }
        for(int i = 1;i < m;i++){
            for(int j = 0;j < n;j++){
                if(matrix[i][j] == '1'){
                    heights[i][j] = heights[i-1][j]+1;
                }
            }
        }
        for(int i = 0;i < m;i++){
            max_num = max(max_num,process(heights,i));
        }
        return max_num;
    }

    int process(vector<vector<int>> &heights,int row){
        int col = heights[row].size();
        stack<int> s;
        int i = 0,max_num = 0;
        // 下面一段代碼着重注意下
        while(i < col){
            if(s.empty() || heights[row][s.top()] < heights[row][i]){
                s.push(i);
                i++;
            }
            else{
                int cur = s.top();s.pop();
                max_num = max(max_num, heights[row][cur]*(s.empty()?i:(i-s.top()-1)));
            }
        }
        return max_num;
    }
};

 

思路2:暴力往前走

遞增時,直接往後走

非遞增時,往前回溯,逐個計算矩形面積

class Solution {
public:
    int maximalRectangle(vector<vector<char>>& matrix) {
        if(matrix.empty() || matrix[0].empty()) return 0;
        int m = matrix.size(),n = matrix[0].size(),max_num = 0;
        vector<vector<int>> heights(m,vector<int>(n+1,0));
        for(int j = 0;j < n;j++){
            if(matrix[0][j] == '1'){
                heights[0][j] = 1;
            }
        }
        for(int i = 1;i < m;i++){
            for(int j = 0;j < n;j++){
                if(matrix[i][j] == '1'){
                    heights[i][j] = heights[i-1][j]+1;
                }
            }
        }
        for(int i = 0;i < m;i++){
            max_num = max(max_num,process(heights,i));
        }
        return max_num;
    }

    int process(vector<vector<int>> &heights,int row){
        int col = heights[row].size();
        stack<int> s;
        int i = 0,max_num = heights[row][0];
        while(i < col){
            // 遞增直接往後走
            while(i+1 < col && heights[row][i] < heights[row][i+1]) i++;
            // 非遞增時,往前回溯,計算矩形面積
            int cur = heights[row][i];
            for(int j = i;j >= 0;j--){
                cur = min(cur,heights[row][j]);
                max_num = max(max_num,cur*(i-j+1));
            }
            i++;
        }
        return max_num;
    }
};

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章