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. mysql错误集合

    一.This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its de 错误解决办法 这是我们开启了bin-log ...

  2. 如何在QFileSystemModel中显示文件夹的大小

    在Qt里面,有一种Model/View框架,Model负责收集信息,View负责显示信息.QFileSystemModel可以读取文件大小,但是默认情况下不能读取文件夹大小. QFileSystemM ...

  3. Delphi 10.3.1 Secure File Sharing解决应用间文件共享

    Delphi 10.3.1 为Android项目提供了Secure File Sharing选择项,默认是False.这一项是设置什么呢? 原来,Android 7及以后的版本,为了加强OS的安全性, ...

  4. TP5 强制下载PDF

    为什么叫强制下载  因为你点击你的PDF文件路劲的话   浏览器是默认字网页上打开,而不是下载 我们需要做的就是 修改header头信息  使其变为下载状态 //下载PDF public functi ...

  5. 周强201771010141《面向对象程序设计(java)》第一周学习总结

    周强201771010141<面向对象程序设计(java)>第一周学习总结 第一部分:课程准备部分 填写课程学习 平台注册账号, 平台名称 注册账号 博客园:www.cnblogs.com ...

  6. ios MQTT协议的实际应用

    1,创建单视图项目,pod search mqtt找到一个库,然后在项目目录下pod init 出Podfile 2,pod install 安装mqtt库 3,主要代码: #import " ...

  7. hsdfz -- 6.16 -- day1

    恩这回不写游记了 按照老师要求记录今天的心里路程:这题似乎可做期望得分150->日部分分似乎不是很显然->a题似乎是结论题,大力猜一波结论->过不了样例,先看b题->b题动态树 ...

  8. webpack学习笔记(三)

    访问网址: https://github.com/webpack/analyse "scripts": { "dev-build": "webpack ...

  9. linux --- 1.初始linux

    一.计算机简单认识 1.服务器的硬件 ①输入单元:键盘,鼠标,读卡器,触摸屏,手写板 ②主机部分:主板,cpu,显卡,内存条,硬盘,网卡,声卡,电池,散热器 ③输出单元:显示器,打印机 2.内存,cp ...

  10. 解决java.lang.IllegalArgumentException: No converter found for return value of type 的问题

    controller返回一个dto,并且定义了@ResponseBody注解,表示希望将这个dto对象转换为json字符串返回给前端,但是运行时报错:nested exception is java. ...