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的加强版: 首先对于每一行,分 ...
随机推荐
- 读取txt数据存入数据库中
http://blog.csdn.net/daditao/article/details/18899469
- WebView性能、体验分析与优化
育新 徐宏 嘉洁 ·2017-06-09 20:03 在App开发中,内嵌WebView始终占有着一席之地.它能以较低的成本实现Android.iOS和Web的复用,也可以冠冕堂皇的突破苹果对热更新的 ...
- 磁性窗体设计C#(二)
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- phpcms 加载微信类库,生成签名
在phpcms 中 pc_base:load_config(文件名) 用于加载配置文件,配置文件存放于phpcms目录下的caches/configs中 在控制器新增加载微信类库的方法: /** * ...
- 使用pip命令自动生成项目安装依赖清单
Python项目中经常会带requirements.txt文件,里面是项目所依赖的包的列表,也就是依赖关系清单,这个清单也可以使用pip命令自动生成. pip命令: 1 pip freeze > ...
- 深入理解synchronized方法同步的是方法还是对象?
一.运用synchronized关键字 首先我们来看看一个多线程中线程不安全的列子 代码如下: 共享数据类: public class NotSynchronizated extends Thread ...
- cocos2dx开发之util类&方法——字符串替换
/*将originStr字符串中的searchStr替换成replaceStr*/ std::string str_replace(std::string originStr,std::string ...
- Linux设备驱动模型之platform(平台)总线详解
/********************************************************/ 内核版本:2.6.35.7 运行平台:三星s5pv210 /*********** ...
- web前端3.0时代,“程序猿”如何“渡劫升仙”?
世界上目前已经有超过18亿的网站.其中只有不到2亿的网站是活跃的.且每天都有几千个新网站不断被创造出来. 2017年成果显著,网络上出现了像Vue这样的新JavaScript框架:基于用户体验流程的开 ...
- AR外包,就找北京动点软件,长年承接AR外包(案例丰富可签合同,内附案例演示)
北京团队长年承接AR项目外包 咨询QQ:372900288 微信:liuxiang0884 以下是AR项目案例演示,索取更多案例请通过以上方式在线联系我们