leetCode 85.Maximal Rectangle (最大矩阵) 解题思路和方法
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
思路:此题的意思是给一个为0或1的矩阵,求所有为1组成的最大矩阵的面积。
此题能够巧妙转化为求最大直方图面积的问题。
public class Solution {
//其思想是将每一列的1逐行相加,遇0为0。遇1相加
//然后转化为求每一行的最大直方图面积的求解
//最后求出最大全为1的矩形面积
public int maximalRectangle(char[][] matrix) {
//边界条件
if(matrix.length == 0 || matrix[0].length == 0){
return 0;
}
/**
* 按列将每列的1逐行相加
*/
for(int j = 0; j < matrix[0].length; j++){
for(int i = 1; i < matrix.length; i++){
if(matrix[i][j] != '0'){//不是0才相加。是0无论
matrix[i][j] = (char) (matrix[i-1][j] + 1);
}
}
}
int maxArea = 0;//最大矩形面积
for(int i= 0; i < matrix.length; i++){
maxArea = max(matrix[i],maxArea);//循环求最大
}
return maxArea;
}
/**
* 依据每行。求最大直方图的面积
* @param height char数组
* @param maxArea 当前最大面积
* @return
*/
private int max(char[] height,int maxArea){
if(height.length == 0){//为空直接返回
return maxArea;
}
/**
* 两个栈,分别存在高度和索引
*/
Stack<Character> stHeight = new Stack<Character>();
Stack<Integer> stIndex = new Stack<Integer>();
/**
* 遍历
*/
for(int i = 0 ; i < height.length; i++){
//栈为空。或者高度比栈顶高度大,入栈
if(stHeight.isEmpty() || height[i] > stHeight.peek()){
stHeight.push(height[i]);
stIndex.push(i);
}else if(height[i] < stHeight.peek()){//高度比栈顶高度小
int lastIndex = 0;//最后的索引值
while(!(stHeight.isEmpty()) && height[i] < stHeight.peek()){
lastIndex = stIndex.pop();
int area = (stHeight.pop() - '0')*(i - lastIndex);//计算面积
maxArea = maxArea < area ? area:maxArea;
}
stHeight.push(height[i]);//当前值入栈
stIndex.push(lastIndex);//最小索引入栈
}
}
//假设栈不为空。继续计算
while(!(stHeight.isEmpty())){
int area = (stHeight.pop() - '0')*(height.length - stIndex.pop());
maxArea = maxArea < area ? area:maxArea;
}
return maxArea;
}
}
详细代码和思路例如以下:
leetCode 85.Maximal Rectangle (最大矩阵) 解题思路和方法的更多相关文章
- leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- 求解最大矩形面积 — leetcode 85. Maximal Rectangle
之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...
- LeetCode (85): Maximal Rectangle [含84题分析]
链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...
- [LeetCode] 85. Maximal Rectangle 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...
- leetcode[85] Maximal Rectangle
给定一个只含0和1的数组,求含1的最大矩形面积. Given a 2D binary matrix filled with 0's and 1's, find the largest rectangl ...
- leetCode 74.Search a 2D Matrix(搜索二维矩阵) 解题思路和方法
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- leetCode 75.Sort Colors (颜色排序) 解题思路和方法
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- leetCode 86.Partition List(分区链表) 解题思路和方法
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- leetCode 15. 3Sum (3数之和) 解题思路和方法
3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...
随机推荐
- ubuntu16.04下snort的安装(官方文档安装)(图文详解)
不多说,直接上干货! 最近为了科研,需要安装和使用Snort. snort的官网 https://www.snort.org/ Snort作为一款优秀的开源主机入侵检测系统,在windows和Linu ...
- springmvc 1.接受日期类型的参数 2.后台返回json串的格式处理(返回json串null值处理为"")
springmvc中的配置: <bean id="dateConvert" class="com.iomp.util.DateConvert"/> ...
- 有关 IE11
IE10/IE11 中 99% 的 CSS3 属性不需要写 -ms- 前缀: IE9/IE10/IE11 默认开启 GPU 加速,效果比同期 Chrome 版本(如 35)流畅: IE11 的字体渲染 ...
- sql排名函数--四个
1 row_number 2 rank 3 dense_rank 4 ntile 例子如下: select * into #MyTablefrom(select '语文' as 课程,70 as 成绩 ...
- Linux下安装使用MySQL
网上找那些安装教程比较多的版本,版本只要不是太旧就行. 下载mysql 5.6.28 通用版64位二进制版,二进制版相当于windows的安装包,可以直接安装,如果是源码版,还需要编译后再进行安装. ...
- linux 下 .sh 文件语法
转自:http://blog.sina.com.cn/s/blog_54f82cc201010hfz.html 介绍: 1 开头 程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh ...
- 命令alias、gerp、find及基础Shell脚本
一. alias 命令:系统设置命令别名 用法:alias [-p] [name[=value] ... ] 注意‘=’和字符串之间不能包含空格 显示当前设置的别名:alias 或 alias ...
- [NOI2003]逃学的小孩(树的直径)
[NOI2003]逃学的小孩 题目描述 Chris家的电话铃响起了,里面传出了Chris的老师焦急的声音:"喂,是Chris的家长吗?你们的孩子又没来上课,不想参加考试了吗?"一听 ...
- 极速响应Excel数据报表请求的一种方法
摘要 通过缓存和维护Excel Workbook实例,极速响应Excel数据报表请求. 这是一个真实的大数据"云计算"项目中的解决方案,在给定的时间和资源下,只有这种方法是最简单并 ...
- CMSIS-RTOS功能概述
以下列表简要概述了所有CMSIS-RTOS功能.标有$的函数是可选的.特定的CMSIS-RTOS实现可能无法提供所有功能,但osFeatureXXXX定义明确指出了这一点. 注意 RTX实现不支持的功 ...