zoukankan      html  css  js  c++  java
  • LeetCode Maximal Rectangle

    >Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.

    class Solution {
    public:
        int maximalRectangle(vector<vector<char> > &matrix) {
            int rows = matrix.size();
            if (rows == 0) return 0;
            int cols = matrix[0].size();
            if (cols == 0) return 0;
            cols++;
            
            int* memo = new int[cols * cols];
            for (int i=cols * cols - 1; i>=0; i--) memo[i] = 0;
            for (int i=1; i<cols; i++) memo[i * cols + i - 1] = 1;
            int max_rect = 0;
            for (int i=0; i<rows; i++) {
                vector<char>& row = matrix[i];
                for (int j = 1; j<cols; j++) {
                    int base = j * cols;
                    for (int k = j; k<cols; k++) {
                        if (row[k - 1] == '1' && memo[base + k - 1] > 0) {
                            int cur_rect = ++memo[base + k] * (k - j + 1);
                            if (cur_rect > max_rect) max_rect = cur_rect;
                        } else {
                            memo[base + k] = 0;
                        }
                    }
                }
            }
            delete[] memo;
            return max_rect;
        }
    };

    O(n^3)暴力,用一个memo数组记录各个区段上的高度(memo[i+1][j+1]表示扫描到matrix中某一行时,该行[x_row][i...j]区间内的最低高度),用时250+ms,按照leetcode的一般时间范围,肯定有巧妙的解法,去zhuli哥看了一下,结合做max rectangular area in histogram那题可以把时间降到O(n^2),用时70+ms,缺乏灵活运用能力啊!下面给出代码

        int maximalRectangle(vector<vector<char> > &matrix) {
            int rows = matrix.size();
            if (rows == 0) return 0;
            int cols = matrix[0].size();
            if (cols == 0) return 0;
    
            vector<int> height(cols, 0);
            vector<int> L, R;
            L.resize(cols), R.resize(cols);
            
            int max_rect = 0;
            
            for (int i=0; i<rows; i++) {
                for (int j=0; j<cols; j++) {
                    height[j] = matrix[i][j] == '1' ? height[j] + 1 : 0;
                }
                for (int j=0; j<cols; j++) {
                    L[j] = j;
                    while (L[j] - 1 >= 0 && height[L[j] - 1] >= height[j]) {
                        L[j] = L[L[j] - 1];
                    }
                }
                for (int j=cols-1; j>=0; j--) {
                    R[j] = j;
                    while (R[j] + 1 < cols && height[R[j] + 1] >= height[j]) {
                        R[j] = R[R[j] + 1];
                    }
                }
                for (int j=0; j<cols; j++) {
                    int rect = (R[j] - L[j] + 1) * height[j];
                    if (rect > max_rect) max_rect = rect;
                }
            }
            return max_rect;
        }

    接着用那题maximum rectangle in histogram中stack版本来实现:

    class Solution {
    public:
        int maximalRectangle(vector<vector<char>>& matrix) {
            int area = 0;
            int rows = matrix.size();
            if (rows < 1) {
                return area;
            }
            int cols = matrix[0].size();
            if (cols < 1) {
                return cols;
            }
            vector<int> h(cols + 1, 0);
            
            for (int i=0; i<rows; i++) {
                for (int j=0; j<cols; j++) {
                    h[j] = matrix[i][j] == '0' ? 0 : (h[j] + 1);
                }
                area = max(area, max_rect(h));
            }
            return area;
        }
        
        int max_rect(vector<int>& height) {
            int len = height.size();
            stack<int> pos;
            int maxarea = 0;
            for (int i=0; i<len; i++) {
                while (!pos.empty() && height[i] < height[pos.top()]) {
                    int h = height[pos.top()];
                    pos.pop();
                    int w = i - (pos.empty() ? -1 : pos.top()) - 1;
                    maxarea = max(maxarea, h * w);
                }
                pos.push(i);
            }
            return maxarea;
        }
    };

    参考:

      http://www.cnblogs.com/zhuli19901106/p/3570175.html

  • 相关阅读:
    【VB.NET】ADO.Net学习(二)DataReader填充DataTable
    【ASP.NET】基础补习之ViewState
    Design Patterns(二):Singleton PatternVB代码
    利用Firefox插件管理你的博客
    【ASP.NET】基础补习之验证控件
    基于.net开发平台项目案例集锦[转]
    Design Patterns(四):Builder PatternVB代码
    【ASP.NET】基础补习之FileUpload
    [转]如何提高自己的核心竞争力
    LeetCode: Pascal's Triangle II
  • 原文地址:https://www.cnblogs.com/lailailai/p/3720613.html
Copyright © 2011-2022 走看看