首页 > 试题广场 >

最大的长方形

[编程题]最大的长方形
  • 热度指数:10732 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
给出一个只包含0和1的二维矩阵,找出最大的全部元素都是1的长方形区域,返回该区域的面积。
/*
	 * 本体解法基于84. Largest Rectangle in Histogram
	 * 把每一行看成求直方图中最大矩形面积问题
	 */
	public int maximalRectangle(char[][] matrix) {
		if (matrix == null || matrix.length == 0 || matrix[0].length == 0)
			return 0;
		int m = matrix.length, n = matrix[0].length;
		int max = 0;
		int[] h = new int[n];
		for (int i = 0; i < m; i++) {
			Stack<Integer> stack = new Stack<Integer>();
			stack.push(-1);
			for (int j = 0; j < n; j++) {
				if (matrix[i][j] == '1')
					h[j] += 1;
				else
					h[j] = 0;

			}
			for (int j = 0; j < n; j++) {
				while (stack.peek() != -1 && h[j] < h[stack.peek()]) {
					int top = stack.pop();
					max = Math.max(max, (j - 1 - stack.peek()) * h[top]);
				}
				stack.push(j);
			}
			while (stack.peek() != -1) {
				int top = stack.pop();
				max = Math.max(max, (n - 1 - stack.peek()) * h[top]);
			}
		}
		return max;
	}

发表于 2017-07-03 09:29:41 回复(9)
算法原型是直方图的最大面积
对于矩阵matrix,逐行构建temp数组,调用 largestRectangleArea   即可。
对matrix第i行构建temp数组方法:对于temp[j],即从matrix[i][j]开始,最多到达matrix[0][j]的连续1个数。
class Solution {
public:
    int maximalRectangle(vector<vector<char>>& matrix) {
        int ret = 0;
        if(matrix.empty()|| matrix[0].empty()){
            return ret;
        }
        
        int m = matrix.size();
        int n = matrix[0].size();
        
        for(int i = 0; i < m; i++){
            vector<int> temp(n, 0);
            for(int j = 0; j < n; j++){
                int r = i;
                while(r>=0 && matrix[r][j] == '1'){
                    temp[j]++;
                    r--;
                }
            }
            ret = max(ret, largestRectangleArea(temp));
        }
        return ret;
    }
    int largestRectangleArea(vector<int>& heights) {
        heights.push_back(0);
        int result = 0;
        stack<int> s;
        for(int i = 0; i < heights.size(); ){
            if(s.empty() || heights[i] >= heights[s.top()]){
                s.push(i);
                i++;
            }else{
                int temp = s.top();
                s.pop();
                result = max(result, (s.empty() ? i : i-s.top()-1)*heights[temp]);
            }
        }
        return result;
    }
};

编辑于 2016-09-02 16:59:25 回复(1)
//参考最高票Java版答案,主要思路就是直方图求最大面积
class Solution {
public:
    int maximalRectangle(vector<vector<char> > &matrix) {
        if(matrix.size()==0)
            return 0;
        int m = matrix.size();
        int n = matrix[0].size();
        vector<int> h(n);
        int maxS = 0;
        int num;
        stack<int> st;
        st.push(-1);
        for(int i=0;i<m;i++)
        {
              //求当前第i行往上连续1的个数,不连续就置为0,方便下一行统计
              //j表示列号,即直方图的连续序号
            for(int j=0;j<n;j++)
            {
                if(matrix[i][j]=='1')
                    h[j]++;
                else
                    h[j] = 0;
            }
            for(int j=0;j<n;j++)
            {
                      //这里主要思路是遇到上升序列就入栈,遇到下降序列就计算当前前一个直方图(即当前栈顶序号)
                      //到所有依次出栈(即降序且大于j指向直方图的高度)直方图的距离,然后乘以出栈直方图的高度,
                       //即为当前的面积(不一定最大),剩下的序列依然是升序的,迭代下去
                while(st.top()!=-1 && h[j]<h[st.top()])
                {
                    num = st.top();
                    st.pop();
                    maxS = max(maxS,(j-1-st.top())*h[num]);
                }
                st.push(j);
            }
               //计算栈中最后一个上升序列的面积(方法同上)
            while(st.top()!=-1)
            {
                num = st.top();
                st.pop();
                maxS = max(maxS,(n-1-st.top())*h[num]);
            }
        }
        return maxS;
    }
};

编辑于 2019-01-23 17:06:20 回复(1)
    /*
    * 目的:最大区域面积
    * 方法:参考leetCode的答案,将每一行都看成一个直方图,最后求直方图所能组成的最大面积
    */
    int maximalRectangle(vector<vector<char> > &matrix) {
        int maxVal = 0;
        int row = matrix.size();
        if (row==0)
            return 0;
        int col = matrix[0].size();
        stack<int>store;
        vector<int>height(col);
        for (int i = 0; i < row; ++i){
            for (int j = 0; j < col;++j){
                if (matrix[i][j] == '1'){
                    height[j]++;
                }
                else{
                    height[j] = 0;
                }
            }
            for (int j = 0; j < col; ++j){
                while(!store.empty() && height[j]<height[store.top()]){
                    int index = store.top();
                    store.pop();
                    int k = store.empty()?-1:store.top();
                    int area = (j-k-1)*height[index];
                    maxVal = max(area,maxVal);
                }
                store.push(j);
            }
            while(!store.empty()){
                int index = store.top();
                store.pop();
                int k = store.empty()?-1:store.top();
                int area = (col-k-1)*height[index];
                maxVal = max(area,maxVal);
            }
        }
        return maxVal;
    }
发表于 2019-12-09 09:11:24 回复(0)
//采用单调递增栈
/*
[
  ["1","0","1","0","0"],
  ["1","0","1","1","1"],
  ["1","1","1","1","1"],
  ["1","0","0","1","0"]
]
转换为:
[
  [1,0,1,0,0],->最大为1
  [2,0,2,1,1],->最大为3
  [3,1,3,2,2],->最大为6
  [4,0,0,3,0],->最大为4
]
*/
class Solution {
public:
    int maximalRectangle(vector<vector<char>>& matrix) {
        if(matrix.size()==0||matrix[0].size()==0)
            return 0;
        int row=matrix.size(),col=matrix[0].size();
        int res=0;
        vector<int> height(col,0);
        for(int i=0;i<row;i++){
            //计算每一层的积累高
            for(int j=0;j<col;j++){
                if(matrix[i][j]=='0')
                    height[j]=0;
                else
                    height[j]++;
            }
            res=max(res,largestRectangleArea(height));
        }
        return res;
    }
    int largestRectangleArea(vector<int>& heights) {
        if(heights.size()==0)
            return 0;
        //避免边界处理
        heights.push_back(0);
        stack<int> s;//s存储原数组的每个index
        int res=0,cnt=0;
        for(int i=0;i<heights.size();i++){
            //更新矩形的最大面积
            cnt=1;
            while(!s.empty()&&heights[i]<=heights[s.top()]){
                //以出栈元素为高的矩阵无法向左边延申(递增),也无法向大于i延申(if条件)
                int h=heights[s.top()];
                s.pop();
                //s为空,表示h是当前的最小值,可以一直延申到下标i
                int width=s.empty()?i:i-1-s.top();
                res=max(res,h*width);
            }
            s.push(i);
        }
        return res;
    }
};
 
发表于 2019-06-09 20:17:26 回复(0)
class Solution {
public:
    int maximalRectangle(vector<vector<char> > &matrix) {
        int rows = matrix.size();
        if(rows <= 0 ) return 0;
        int cols = matrix[0].size(),maxH[cols];
        int maxArea = 0;
        for (int m = 0; m < rows; m ++) {
            for (int i = 0; i < cols; i++) {
                if (matrix[m][i] == '1') maxH[i] = 1;
                else maxH[i] = 0;
            }
            for (int j = 0; j < cols; j++) {
                int w = 1,l = j - 1, r = j + 1;
                while (l >= 0 && maxH[l] <= maxH[j] && maxH[l] > 0)   {l--;w++;}
                while (r < cols && maxH[r] <= maxH[j] && maxH[r] > 0) {r++;w++;}
                if (w * maxH[j] > maxArea) maxArea = w * maxH[j];
            }
            for (int i = m+1; i < rows; i++) {
                for (int j = 0; j < cols; j++) {
                    if (matrix[i][j] == '1' && maxH[j] > 0 ) {maxH[j]++;} 
                    else {maxH[j] = 0;}
                }
                for (int j = 0; j < cols; j++) {
                    int w = 1,l = j - 1, r = j + 1;
                    while (l >= 0 && maxH[l] <= maxH[j] && maxH[l] > 0) {l--;w++;}
                    while (r < cols && maxH[r] <= maxH[j] && maxH[r] > 0) {r++;w++;}
                    if (w * maxH[j] > maxArea) maxArea = w * maxH[j];
                }
            }
        }
        return maxArea;
    }
};

发表于 2019-04-15 15:18:06 回复(0)
class Solution {
public:
    int maximalRectangle(vector<vector<char> > &matrix) {
        int max_rec_area=0;
        if(matrix.size()==0||matrix[0].size()==0)
            return 0;
        vector<int> height(matrix[0].size(),0);
        stack<int> s;
        int n=matrix[0].size();
        for(int i=0;i<matrix.size();++i){
           for(int j=0;j<matrix[0].size();++j){
            if(matrix[i][j]=='0')
                 height[j]=0;
            else
                 height[j]+=1;
            while(!s.empty()&&height[s.top()]>=height[j])
            {
                int h=height[s.top()];
                s.pop();
                max_rec_area=max(max_rec_area,(j-1-(s.empty()?(-1):s.top()))*h);
            }
            s.push(j);
        }
            while(!s.empty()){
                int h=height[s.top()];
                s.pop();
                max_rec_area=max(max_rec_area,(n-1-(s.empty()?(-1):s.top()))*h);
            }
            
        }
        return max_rec_area;
    }
};
发表于 2018-05-20 11:57:48 回复(0)
class Solution {
public:
    int maximalRectangle(vector<vector<char> > &matrix) {
        int m = matrix.size();
        int n = matrix[0].size();
        if(m==0 || n==0)
            return 0;
        
        int Max = (matrix[0][0]==1?1:0);
        int l = 0, r = n;         vector<int> h(n,0), left(n,0), right(n,n);
        
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
                h[j] = (matrix[i][j]=='1' ? h[j]+1:0);
                         l = 0;
            for(int j=0;j<n;j++)
            {
                if(matrix[i][j] == '1')
                    left[j] = max(left[j], l);
                else{
                    left[j] = 0;
                    l = j + 1;                 }             }                          r = n;             for(int j=n-1;j>=0;j--)             {                 if(matrix[i][j] == '1')                     right[j] = min(right[j], r);                 else{                     right[j] = n;                     r = j;                 }             }                          for(int j=0;j<n;j++)                 Max = max(Max, h[j]*(right[j]-left[j]));         }         return Max;
    }
};

发表于 2017-09-25 01:42:29 回复(0)
class Solution {
public:
    int maximalRectangle(vector<vector<char> > &matrix) {  
    if(matrix.size()==0) 
        return 0; 
    int cols = matrix[0].size(); 
    int* hist = new int[cols]();  
        // 以某一行为起头对应该列的最大矩形(列连续1个数)
    int max_ = 0;
    for(int i=0; i<matrix.size(); i++)  
    {  
        for(int j=0; j<cols; j++)  
        {  
            if(matrix[i][j]=='1')  
                hist[j] += 1;  
            else  
                hist[j] = 0;  
        }
        max_ = max(max_, maxRectInHistogram(hist, cols) );  
    }
    delete [] hist;
    hist = nullptr;
    return max_;  
}  
int maxRectInHistogram(int hist[], int n)
{    
        int* arr = new int[n];// 申请一个额外的数组  以i为结尾的最大面积
        arr[0] = hist[0];    
        int m = hist[0]; // 最大面积    
        for(int i=1; i<n; i++)    
        {    
                arr[i] = hist[i];
                int temp = hist[i];
                for(int j=i-1; j>=0 && hist[j] > 0; j--)
                {
                    temp = min(temp,hist[j]);
                    arr[i] = max(arr[i],temp*(i-j+1));
                }
                if(m < arr[i])
                        m = arr[i];
        }
        delete [] arr;
        arr = nullptr;
        return m;    
}
};

发表于 2017-08-18 16:34:55 回复(0)
// 从有“1”的地方开始搜索
public class Solution {
    public int maximalRectangle(char[][] matrix) {
        if(matrix == null || matrix.length <= 0 || matrix[0].length <= 0)
        	return 0;
        
        int max = 0;
        for(int i = 0; i < matrix.length; i++){
        	for(int j = 0; j < matrix[0].length; j++){
        		if(matrix[i][j] == '0')
        			continue;
        		int width = Integer.MAX_VALUE;
        		for(int p = i; p < matrix.length; p++){
        			if(matrix[p][j] == '0')
            			break;
        			for(int q = j; q < matrix[0].length; q++){
        				if(matrix[p][q] == '0' || q == matrix[0].length - 1){
        					int curwid;
        					if(matrix[p][q] == '1')
        						curwid = q - j + 1;
        					else 
        						curwid = q - j;
        					if(curwid < width)
        						width = curwid;
        					int curS = width * (p - i + 1);
        					if(curS > max)
        						max = curS;
        					break;
        				}
        			}
        		}        		
        	}
        }
        return max;
    }
}

发表于 2017-05-15 16:02:43 回复(0)
    int largestRectangleArea(vector<int> &height) {
        stack<int> inds;
        int max=0;
        height.push_back(0);
        for(int i=0; i<height.size(); ++i){
            if(inds.empty()||height[i]>=height[inds.top()])
                inds.push(i);
            else{
                int top=inds.top();
                inds.pop();
                max=std::max(max, height[top]*(inds.empty()?i:(i-inds.top()-1)));
                --i;
            }
        }
        return max;
    }
    
    int maximalRectangle(vector<vector<char> > &matrix) {
        if(matrix.empty())
            return 0;
        vector<vector<int>> hist(matrix[0].size(), vector<int>(matrix.size(), 0));
        for(int c=0; c<matrix[0].size(); ++c)
            for(int r=0; r<matrix.size(); ++r)
                if(matrix[r][c]=='1')
                    hist[c][r]=find_if_not(matrix[r].begin()+c, matrix[r].end(), [](char x){return x=='1';})-matrix[r].begin()-c;
        int maxArea=0;
       for(auto h:hist)
            maxArea=max(maxArea, largestRectangleArea(h));
        return maxArea;
    }

发表于 2017-05-12 18:23:36 回复(0)
//最渣的版本
class Solution {
public:
    int maximalRectangle(vector<vector<char> > &matrix) {
        if(matrix.size()==0) return 0;
        int rows=matrix.size();
        int cols=matrix[0].size();
        int MAX=0;
        vector<int >temp(cols,0);
        vector<vector<int > >result;
        for(int i=0;i<rows;i++)
        {
            for(int j=0;j<cols;j++)
            {
                
                temp[j]=matrix[i][j]-'0'+(matrix[i][j]-'0')*temp[j];
            }
            MAX=max(largestRectangleArea(temp),MAX);
        }
        return MAX;
    }
    int largestRectangleArea(vector<int> &height) {
        int MAXAREA=0;
        for(int i=0;i<height.size();i++)
        {   int MIN=height[i];
            for(int j=i;j<height.size();j++)
            {
                
                MIN=min(MIN,height[j]);
                int CurArea=MIN*(j-i+1);
                if(CurArea>MAXAREA)
                {
                    MAXAREA=CurArea;
                }
            }
        }
        return MAXAREA;
    }
};

发表于 2018-03-20 15:13:19 回复(0)

从这个问题想到单调栈结构确实有点不容易,我们举例说明:

  1. 0 0 1 1 0 1 1
  2. 0 0 1 1 1 0 1
  3. 0 1 1 1 1 1 0
  4. 1 0 0 0 0 1 1

然后将每个元素转化为该元素及上面相连的1的个数和:

  1. 0 0 1 1 0 1 1
  2. 0 0 2 2 1 0 2
  3. 0 1 3 3 2 1 0
  4. 1 0 0 0 0 2 1

以第3行为例,转化成柱形图,下图:

图片说明

是不是很熟悉呢?对的,其实这就是单调栈结构的一个典型应用,以求上面图片中最大矩形面积为例,单调栈的实现如下(建议作为单调栈题目的模版去记忆):

int monotoneStack(vector<int> &vec) {
    stack<int> st;
    vec.push_back(0);   // ➤ 添加一个0可以在循环的最后一步清空栈
    int maxVal = 0;
    for (int i = 0; i < vec.size(); ++i) {
        // 这里用单调递增栈
        while (!st.empty() && vec[i] < vec[st.top()]) {
            int height = vec[st.top()];
            st.pop();
            int left = st.empty() ? -1 : st.top();
            int width = i - (left+1);
            maxVal = max(maxVal, height * width);
        }
        st.push(i);
    }
    return maxVal;
}

完整代码如下:

//
// Created by jt on 2020/9/2.
//
#include <vector>
#include <stack>

using namespace std;

class Solution {
public:
    int maximalRectangle(vector<vector<char> > &matrix) {
        if (matrix.empty() || matrix[0].empty()) return 0;
        vector<vector<int> > tmp(matrix.size(), vector<int>(matrix[0].size()));
        // 第一次遍历:将char型数组转化为int型数组
        for (int i = 0; i < matrix.size(); ++i) {
            for (int j = 0; j < matrix[0].size(); ++j) {
                tmp[i][j] = matrix[i][j] - '0';
            }
        }

        // 第二次遍历:转化矩阵
        for (int i = 1; i < tmp.size(); ++i) {
            for (int j = 0; j < tmp[0].size(); ++j) {
                if (tmp[i][j] == 1) tmp[i][j] = tmp[i - 1][j] + 1;
            }
        }

        // 第三次遍历:单调递增栈求最大矩阵面积
        int maxVal = 0;
        for (int i = 0; i < tmp.size(); ++i) {
            maxVal = max(maxVal, monotoneStack(tmp[i]));
        }
        return maxVal;
    }

private:
    int monotoneStack(vector<int> &vec) {
        stack<int> st;
        vec.push_back(0);   // ➤ 添加一个0可以在循环的最后一步清空栈
        int maxVal = 0;
        for (int i = 0; i < vec.size(); ++i) {
            // 这里用单调递增栈
            while (!st.empty() && vec[i] < vec[st.top()]) {
                int height = vec[st.top()];
                st.pop();
                int left = st.empty() ? -1 : st.top();
                int width = i - (left+1);
                maxVal = max(maxVal, height * width);
            }
            st.push(i);
        }
        return maxVal;
    }
};
发表于 2020-09-02 19:32:44 回复(0)
class Solution {
public:
    int maximalRectangle(vector<vector<char> > &matrix) {
        if (matrix.empty())
            return 0;
        int row = matrix.size();
        int clo = matrix.front().size();
        int res = 0;
        vector<int> temp(clo);
        for (int i = 0; i < row; i++){
            for (int j = 0; j < clo; j++){//求出每一行的直方图高度数组
                if (matrix[i][j] == '0')
                    temp[j] = 0;
                else if (matrix[i][j] == '1')
                    temp[j] += 1;
            }
            for (int j = 0; j < clo; j++){//遍历该行,找出最大面积
                if (temp[j] == 0)
                    continue;
                int left = j, right = j;
                while (left >= 0 && temp[left] >= temp[j]) left--;
                while (right < clo && temp[right] >= temp[j]) right++;
                res = res > (right - left - 1)*temp[j]? res:(right - left - 1)*temp[j];
            }
        }
        return res;
    }
};
发表于 2020-08-27 14:08:49 回复(0)
借用上述的思路,将每一行当成直方图,来求解最大的矩形面积
采用二维动态规划来解决求最大直方图面积
class Solution {
public:
    int RectSquare(vector<int> &vec){
        int res = 0;
        vector<vector<int> > dp(vec.size(), vector<int>(vec.size(), 0));
        for(int i = 0; i < vec.size(); ++i){
            dp[i][i] = vec[i];
        }
        //dp[i][j] 表示i到j的最大面积,dp[i][j + 1] = max(dp[i][j], min(i到j的元素)*(j-i+1))
        for(int i = 0; i < vec.size(); ++i){
            int cur = vec[i];
            res = max(res, cur);
            for(int j = i + 1; j < vec.size(); ++j){
                cur = min(cur, vec[j]);
                dp[i][j] = max(dp[i][j-1], cur * (j - i + 1));
                res = max(res, dp[i][j]);
            }
        }
        return res;
    }
    int maximalRectangle(vector<vector<char> > &matrix) {
        int res = 0;
        if(matrix.empty()) return res;
        int m = matrix.size(), n = matrix[0].size();
        vector<int> tmp(n, 0);
        for(int i = 0; i < m; ++i){
            for(int j = 0; j < n; ++j){
                if(matrix[i][j] == '1'){
                    tmp[j] = tmp[j] + 1;
                }
                else tmp[j] = 0;
            }
            res = max(res, RectSquare(tmp));
        }
        return res;
    }
};


发表于 2020-06-22 10:40:56 回复(0)
//利用Largest Rectangle in Histogram的思想,可参考
//https://www.bilibili.com/video/BV1eb411M7Gm?from=search&seid=5986427457100938076
//若matrix有m行,则可以把问题分解成m个Largest Rectangle in Histogram问题
//从上到下遍历matrix的每一行,第i行往上的部分可以看做是Largest Rectangle in Histogram问题中的一个柱形图

class Solution {
public:
    int maximalRectangle(vector<vector<char> > &matrix) {
        int m=matrix.size();
        if(m==0)
            return 0;
        int n=matrix[0].size();
        stack<int>st;
        st.push(-1);
        int max_area=0;
        vector<int>heights(n,0);
        for(int i=0;i<m;++i)
        {
            for(int j=0;j<n;++j)
            {
                if(matrix[i][j]=='1')
                    heights[j]++;
                else
                    heights[j]=0;
            }
            for(int j=0;j<n;++j)
            {
                while(st.top()!=-1&&heights[j]<heights[st.top()])
                {
                    int index=st.top();
                    st.pop();
                    max_area=max(max_area,heights[index]*(j-1-st.top()));
                }
                st.push(j);
            }
            while(st.top()!=-1)
            {
                int index=st.top();
                st.pop();
                max_area=max(max_area,heights[index]*(n-1-st.top()));
            }
        }
        return max_area;
    }
};

发表于 2020-06-04 20:04:54 回复(0)
class Solution {
public:
    int maximalRectangle(vector<vector<char> > &matrix) {
        int n = matrix.size();
        int m = matrix[0].size();
        if(n == 0 || m == 0)
            return 0;
        int max_rec = 0;
        vector<int> h(m,0);
        for(int i = 0; i < n; ++i){
            for(int j = 0; j < m; ++j){
                if(matrix[i][j] == '1')
                    h[j]++;
                else
                    h[j] = 0;
            }
            stack<int> sta;
            sta.push(-1);
            for(int j = 0; j < m; ++j){
                while(sta.top()!= -1 && h[sta.top()] > h[j]){
                    int tmp = sta.top();
                    sta.pop();
                    max_rec = max(max_rec, (j - sta.top()-1)*h[tmp]);
                }
                sta.push(j);
            }
            while(sta.top()!=-1){
                int tmp = sta.top();
                sta.pop();
                max_rec = max(max_rec, (m - sta.top()-1)*h[tmp]);
            }
        }
        return max_rec;
    }
};

发表于 2020-05-14 22:22:56 回复(0)
做了这么久leetcode终于学到了一种新算法。。。栈解决用o(n)真的很神奇
发表于 2020-03-12 23:05:15 回复(0)
import java.util.Stack;
public class Solution {
    public int maximalRectangle(char[][] matrix) {
        if(matrix==null||matrix.length==0)return 0;
        int row = matrix.length,col = matrix[0].length;
        int max = 0;
        int[] heights = new int[col];
        for(int i = 0;i < row;i++){
            for(int j=0;j<col;j++){
                if(matrix[i][j]=='1')
                    heights[j]++;
                else
                    heights[j] = 0;
            }
            max = Math.max(max,getMaxRectangle(heights));
        }
        return max;
    }
    
    public int getMaxRectangle(int[] heights){
        int n = heights.length;
        int max = 0,j,k,curMax;
        Stack<Integer> stk = new Stack<>();
        for(int i=0;i<n;i++){
            while(!stk.isEmpty()&&heights[stk.peek()]>=heights[i]){
                j = stk.pop();
				k = stk.isEmpty()?-1:stk.peek();
				curMax = (i-k-1)*heights[j];
               // curMax = (i-j)*heights[j];    错误的更新
				max = Math.max(curMax, max);
            }
            stk.push(i);
        }
        while(!stk.isEmpty()){
            j = stk.pop();
            
			k = stk.isEmpty()?-1:stk.peek();
			curMax = (n-k-1)*heights[j];
            //curMax = (n-j)*heights[j];    错误的更新
			max = Math.max(curMax, max);
        }
        return max;
    }
    
}

发表于 2020-02-17 12:49:57 回复(0)
public class Solution {
    public int maximalRectangle(char[][] matrix) {
        if (matrix == null)
            return 0;
        
        int h = matrix.length;
        if (h == 0)
            return 0;
        int w = matrix[0].length;
        if (w == 0)
            return 0;
        
        int[][] dp = new int[h][w];
        int i = h - 1, j = 0;
        while (j < w) {
            dp[i][j] = matrix[i][j] - '0';
            j++;
        }
        for (i = h - 2; i >= 0; i--)
            for (j = 0; j < w; j++)
                dp[i][j] = matrix[i][j] == '0' ? 0 : dp[i + 1][j] + 1;
        
        int max_area = 0, area;
        int jt, ht, wt;
        for (i = 0; i < h; i++)
            for (j = 0; j < w; j++)
                if (matrix[i][j] == '1') {
                    area = dp[i][j];
                    ht = dp[i][j];
                    for (jt = j + 1; jt < w; jt++) {
                        if (matrix[i][jt] == '0')
                            break;
                        wt = jt - j + 1;
                        ht = Math.min(ht, dp[i][jt]);
                        area = Math.max(area, ht * wt);
                    }
                    max_area = Math.max(max_area, area);
                }
        
        return max_area;
    }
}
发表于 2019-12-29 14:54:29 回复(0)

问题信息

难度:
49条回答 20076浏览

热门推荐

通过挑战的用户

查看代码