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 ...
随机推荐
- Struts2国际化-getText()方法
转自https://blog.csdn.net/qq_43560838/article/details/83747604 一:简单理解 国际化简称i18n,其来源是英文单词 international ...
- 4. idea常用快捷键设置(改为eclipse相似)
转自:https://blog.csdn.net/loveer0/article/details/82697877 idea常用快捷键设置(改为eclipse相似) 目录 idea常用快捷键设置改为e ...
- 什么是MySQL?(一)
课程大纲 第一章:初识MySQL 1.1什么是MySQL? 1.2如何安装MySQL? 1.3如何配置MySQL? 1.4如何启动和关闭MySQL? 1.5MySQL的登录和退出 1.6如何 ...
- 集合 List和Set
分别向Set集合以及List集合中添加“A”,“a”,“c”,“C”,“a”5个元素,观察重复值“a”能否在List集合以及Set集合中成功添加. List<String> ls=new ...
- 【DNN控件】
<dnn:DNNDataGrid ID="show" runat="server" DataSourceID="ObjectDataSource ...
- hdu 2018 - 递推
dp[i][1..4] 第i年时年龄为1234的牛的数目 */ #include <cstdio> #include <cstring> ; ]; int main(){ me ...
- sql排名函数--四个
1 row_number 2 rank 3 dense_rank 4 ntile 例子如下: select * into #MyTablefrom(select '语文' as 课程,70 as 成绩 ...
- POJ 3539 Elevator(同余类BFS)
题意 有一部电梯,最初停在1层. 电梯有4个按键,上升a,b,c层,回到一层. 求从一层出发.能到达1~h的哪些楼层. (h<=1018,a,b,c<=105) 题解 这种h能大的图论,一 ...
- 在ubuntu上编译rasbian kernel(for raspberry pi 1)
raspberry pi官网的编译手册写的简洁有力,照着操作即可 https://www.raspberrypi.org/documentation/linux/kernel/building.md ...
- ASP.NET WEB API微信支付通知接口,返回xml数据,微信服务器不识别问题
原文:ASP.NET WEB API微信支付通知接口,返回xml数据,微信服务器不识别问题 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/MrTra ...