class Solution {
public int maximalRectangle(char[][] matrix) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return 0; int[] height = new int[matrix[0].length];
for(int i = 0; i < matrix[0].length; i ++){
if(matrix[0][i] == '1') height[i] = 1;
}
int result = largestInLine(height);
for(int i = 1; i < matrix.length; i ++){
resetHeight(matrix, height, i);
result = Math.max(result, largestInLine(height));
} return result;
} private void resetHeight(char[][] matrix, int[] height, int idx){
for(int i = 0; i < matrix[0].length; i ++){
if(matrix[idx][i] == '1') height[i] += 1;
else height[i] = 0;
}
} public int largestInLine(int[] height) {
if(height == null || height.length == 0) return 0;
int len = height.length;
Stack<Integer> s = new Stack<Integer>();
int maxArea = 0;
for(int i = 0; i <= len; i++){
int h = (i == len ? 0 : height[i]);
if(s.isEmpty() || h >= height[s.peek()]){
s.push(i);
}else{
int tp = s.pop();
maxArea = Math.max(maxArea, height[tp] * (s.isEmpty() ? i : i - 1 - s.peek()));
i--;
}
}
return maxArea;
}
}

参考:https://leetcode.com/problems/maximal-rectangle/discuss/29055/My-java-solution-based-on-Maximum-Rectangle-in-Histogram-with-explanation

补充一个python的实现:

 class Solution:
def maximalRectangle(self, matrix: 'List[List[str]]') -> int:
if matrix == None:
return 0
row = len(matrix)
if row == 0:
return 0
column = len(matrix[0])
if column == 0:
return 0
heights = [0] * column
for i in range(column):
if matrix[0][i] == '':
heights[i] = 1
result = self.largestRectangleArea(heights)
for i in range(1,row):
self.resetHeight(matrix,heights,i)
result = max(result,self.largestRectangleArea(heights))
return result def largestRectangleArea(self, heights: 'List[int]') -> int:
heights.append(0)#默认最后补充一个0,方便统一处理
n = len(heights)
s = []
max_area = 0#最大面积
tp = 0#栈顶索引
area_with_top = 0#临时面积
i = 0
while i < n:
if len(s) == 0 or heights[s[-1]] <= heights[i]:
s.append(i)#栈内记录的是高度递增的索引
i += 1
else:#遇到了高度比当前栈顶元素低的元素时,
tp = s.pop(-1)#清算栈内的元素的高度
if len(s) == 0:
area_with_top = heights[tp] * i#栈内没有元素,则宽度是i
else:#高度是栈顶元素,宽度是i - 1 - 前一个栈顶元素的索引
area_with_top = heights[tp] * (i - s[-1] - 1)
max_area = max(max_area,area_with_top)#更新最大值
return max_area def resetHeight(self,matrix,height,idx):
for i in range(len(matrix[0])):
if matrix[idx][i] == '':
height[i] += 1
else:
height[i] = 0

思路分析:本题以leetcode 84为基础,相当于计算一个柱状图中最大的面积。

如图上所示,对于数组[2,1,5,6,2,3]其最大面积是10。在此基础上,将本题转化为row次的柱状图计算:

按照行“递增”计算4次。

1、[1,0,1,0,0],最大面积是1

2、[1+1,0,1+1,1,1] => [2,0,2,1,1],最大面积是3

3、[1+1+1,1,1+1+1,1+1,1+1] => [3,1,3,2,2],最大面积是6

4、[1+1+1+1,0,0,1+1+1,0] => [4,0,0,3,0],最大面积是4

经过这4次循环,计算完毕。第3次得到的结果,是这个二维数组可构成的最大面积6。

leetcode85的更多相关文章

  1. [Swift]LeetCode85. 最大矩形 | Maximal Rectangle

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...

  2. leetcode85 Maximal Rectangle

    思路: 分别按行拆分后将这一行之前的每列叠加起来,然后使用leetcode84https://www.cnblogs.com/wangyiming/p/9620942.html的思路计算. 实现: # ...

  3. LeetCode85 Maximal Rectangle java题解

    public static int maximalRectangle(char[][] matrix) { int rowNum=matrix.length; if(rowNum==0) return ...

  4. kickstart2019 round_C B. Circuit Board

    思路: 这题应该不止一种解法,其中的一种可以看作是leetcode85https://www.cnblogs.com/wangyiming/p/11059176.html的加强版: 首先对于每一行,分 ...

随机推荐

  1. 【转】Spring-boot 字符集设置 解决乱码方案

    使用spring-boot开发时候,有时候程序没事,往往不经意会造成中文到前端变成乱码(????这样情况) 下面给出spring-boot项目统一字符集设置方案: 1.Spring Boot修改编码方 ...

  2. 安卓开发学习之AutoCompleteTextView

    最近在学习安卓开发,开始是看视频学的,基本上是照着老师的操作来,但其实老师也是按照安卓的开发文档来教的,于是决定试试自己看文档来学. 今天学到AutoCompleteTextView,一上来先按照Li ...

  3. 关于windows下的makefile学习

    文中部分引用自:http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=408225 windows下makefile环境配置见于:http ...

  4. Python全栈之路----常用模块----os模块

    os模块提供了很多允许你的程序和操作系统直接交互的功能 得到当前工作目录,即当前Python脚本工作的目录路径:os.getcwd() #test.py import os print(os.getc ...

  5. Parsing Natural Scenes and Natural Language with Recursive Neural Networks-paper

    Parsing Natural Scenes and Natural Language with Recursive Neural Networks作者信息: Richard Socher richa ...

  6. arduino uno r3的数据类型

    char 一个字节,存储一个字符值.字符文字用单引号写成:'A' unsigned char 无符号,一个字节 byte 一个字节,无符号数, int 2字节,这产生-32768至32767的范围. ...

  7. (27)session(设置值、取值、修改、删除)

    session的由来 Cookie虽然在一定程度上解决了“保持状态”的需求,但是由于Cookie本身最大支持4096字节,以及Cookie本身保存在客户端,可能被拦截或窃取,因此就需要有一种新的东西, ...

  8. 为WebService添加身份验证的两种方法

    方法一:SoapHeader 辅助类:MySoapHeader //SoapHeader 添加引用 using System.Web.Services.Protocols; #region 配置登录标 ...

  9. Excel转datatable

    如果想支持 .xls,.xlsx 两种格式 则必须安装一个exe文件,下载路径https://www.microsoft.com/zh-CN/download/details.aspx?id=1325 ...

  10. 集合总结三(HashMap的实现原理)

    一.概述 二话不说,一上来就点开源码,发现里面有一段介绍如下: Hash table based implementation of the Map interface. This implement ...