leetcode85
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;
}
}
补充一个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的更多相关文章
- [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 ...
- leetcode85 Maximal Rectangle
思路: 分别按行拆分后将这一行之前的每列叠加起来,然后使用leetcode84https://www.cnblogs.com/wangyiming/p/9620942.html的思路计算. 实现: # ...
- LeetCode85 Maximal Rectangle java题解
public static int maximalRectangle(char[][] matrix) { int rowNum=matrix.length; if(rowNum==0) return ...
- kickstart2019 round_C B. Circuit Board
思路: 这题应该不止一种解法,其中的一种可以看作是leetcode85https://www.cnblogs.com/wangyiming/p/11059176.html的加强版: 首先对于每一行,分 ...
随机推荐
- Shell脚本的学习笔记二:字符串
菜鸟教程提供的Shell在线编辑器 Shell 字符串 项目 功能 单引号 原样输出,变量无效.但可用成对单引号嵌套成对单引号输出变量 双引号 定义字符串中附带有变量的命令并且想将其解析后再输出的变量 ...
- java面向对象编程(三)--this
看一段代码:(Demo112.java),先了解为什么要使用this. /* this的必要性 */ public class Demo112{ public static void main(Str ...
- 运维grep语法
grep的语法和用法 grep命令的格式: grep [options] PATTERN [FILE] 其中:1,pattern是用正则表达式书写的模式.2,FILE是要查找的文件,可以是用 ...
- mysql在查询中常见问题汇总
1.从主从表中查询外键内容(常见问题) 从主从表中查询对应的外键,需要指定外键的表,即sno=> student.sno或者score.sno 错误:select sno,sname,degre ...
- 单细胞参考文献 single cell
许多分析软件 : https://github.com/seandavi/awesome-single-cell#software-packages Smart-seq.CEL-seq.SCRB-se ...
- css颜色属性及设置颜色的地方
css颜色属性 在css中用color属性规定文本的颜色. 默认值是not specified 有继承性,在javascript中语法是object.style.color="#FF0000 ...
- 列表:remove/del删除方法中的逻辑“误区”
结果: list_1=["A","B","C","D","E","F",&quo ...
- PythonStudy——函数的使用 Use of functions
# print(a) # a = 10 # 注意:函数必须先定义,后使用 # print(get_water) def get_water(water, money): print('收入你的%d元钱 ...
- 新系统centos7重启网络报错
场景: 在不知名云上新弄的centos7,改了IP之后启动不起来,使用systemctl status network查看结果如下: 排查过程: 1)NetworkManager是否关 ...
- 在 delphiXE 10.2 上安装 FR5.4.6
今天在虚拟机里成功安装了delphiXE 10.2, 然后想把常用的FR也装上,本想偷个懒,在网上找个装上就得了,结果发现必须要在发布的网站注册才能下载⊙︿⊙ 如此麻烦,心里这个不爽啊不爽! 然后自己 ...