Maximal Rectangle

两种方法,第一种参考 http://blog.csdn.net/fightforyourdream/article/details/17711893

Time O(n^3), Space O(n^2)

class Solution {
public:
    int maximalRectangle(vector<vector<char> > &matrix) {
        int rows = matrix.size();
        if(rows == 0) return 0;
        int cols = matrix[0].size();
        
        vector<vector<int> > ones(rows, vector<int>(cols, 0));
        for(int i=0; i<rows; i++)
        {
            for(int j=0; j<cols; j++)
            {
                if(matrix[i][j] == '1')
                {
                    if(j == 0)
                        ones[i][j] = 1;
                    else
                        ones[i][j] = ones[i][j-1]+1;
                }
            }
        }
        
        int max_area = 0;
        
        for(int i=0; i<rows; i++)
        {
            for(int j=0; j<cols; j++)
            {
                if(ones[i][j] != 0)
                {
                    int minWidth = ones[i][j];
                    int idx = i;
                    while(idx >= 0)
                    {
                        minWidth = min(minWidth, ones[idx][j]);
                        int area = minWidth * (i-idx+1);
                        max_area = max(max_area, area);
                        idx--;
                    }
                }
            }
        }
        return max_area;
    }
};

第二种,利用了 Largest Rectangle in Histogram的答案。逐行求解。Time O(n^2), Space O(n)。

http://www.cnblogs.com/lichen782/p/leetcode_maximal_rectangle.html

class Solution {
public:
    int maximalRectangle(vector<vector<char> > &matrix) {
        int rows = matrix.size();
        if(rows == 0) return 0;
        int cols = matrix[0].size();
        
        vector<int> heights(cols, 0);
        int result = 0;
        
        for(int i=0; i<rows; i++)
        {
            for(int j=0; j<cols; j++)
            {
                if(matrix[i][j] == '1')
                    heights[j]++;
                else
                    heights[j] = 0;
            }
            int res = largestRectangleArea(heights);
            result = max(result, res);
        }
        return result;
    }
    
    
    int largestRectangleArea(vector<int> &height) {
        const int n = height.size();
        if(n == 0) return 0;
        
        int max_area = 0;
        int area_top = 0;
        int idx = 0;
        
        stack<int> hists;
        while(idx < n)
        {
            if(hists.empty() || height[idx] >= height[hists.top()])
                hists.push(idx++);
            else
            {
                int top = hists.top();
                hists.pop();
                
                int area_top = height[top]*(hists.empty()?idx:(idx-hists.top()-1));
                max_area = std::max(max_area, area_top);
            }
        }
        
        while(!hists.empty())
        {
            int top = hists.top();
            hists.pop();
                
            int area_top = height[top]*(hists.empty()?idx:(idx-hists.top()-1));
            max_area = std::max(max_area, area_top);
        }
        return max_area;
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值