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

  • 相关阅读:
    June 26th 2017 Week 26th Monday
    June 25th 2017 Week 26th Sunday
    June 24th 2017 Week 25th Saturday
    June 23rd 2017 Week 25th Friday
    June 22nd 2017 Week 25th Thursday
    2018最佳网页设计:就是要你灵感爆棚!!!
    图片素材类Web原型制作分享-Pexels
    想要打动HR的心,UX设计师求职信究竟应该怎么写?
    【UXPA大赛企业专访】Mockplus:“设计替代开发”将成为现实
    2018年最好的医疗网站设计及配色赏析
  • 原文地址:https://www.cnblogs.com/lailailai/p/3720613.html
Copyright © 2011-2022 走看看