85. Maximal Rectangle (JAVA)
Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.

Above is a histogram where width of each bar is 1, given height =[2,1,5,6,2,3].

The largest rectangle is shown in the shaded area, which has area =10 unit.
Example:
Input: [2,1,5,6,2,3]
Output: 10
84. Largest Rectangle in Histogram的升级版: 按行按列分别做动态规划
class Solution {
public int maximalRectangle(char[][] matrix) {
if(matrix.length == 0 ) return 0;
int width = matrix[0].length;
int height = matrix.length;
int maxArea = 0;
int area;
//按列动态规划,求出到此位置最多连续为1的次数
int[][] dp = new int[height][width];
for(int i = 0; i < width; i++){
for(int j = 0; j < height; j++){
dp[j][i] = matrix[j][i] - '0';
if(matrix[j][i] == '1' && j > 0){
dp[j][i] += dp[j-1][i];
}
}
}
//按行动态规划,求出最大面积
for(int i = 0; i < height; i++){
area = largestRectangleArea(dp[i]);
if(area > maxArea) maxArea = area;
}
return maxArea;
}
public int largestRectangleArea(int[] heights) {
Stack<Integer> st = new Stack<Integer>();
if(heights.length == 0) return 0;
int leftIndex;
int topIndex;
int area;
int maxArea = 0;
int i = 0;
while(i < heights.length){
if(st.empty() || heights[i] >= heights[st.peek()]){
st.push(i);
i++;
}
else{
topIndex = st.peek();
st.pop();
leftIndex = st.empty()?0:st.peek()+1; //如果是空,说明从头开始的所有高度都比heights[topIndex]高
area = ((i-1)-leftIndex + 1) * heights[topIndex];
if(area > maxArea) maxArea = area;
}
}
while(!st.empty()){ //没有比栈顶元素小的元素让它出栈
topIndex = st.peek();
st.pop();
leftIndex = st.empty()?0:st.peek()+1;
area = ((i-1)-leftIndex + 1) * heights[topIndex];
if(area > maxArea) maxArea = area;
}
return maxArea;
}
}
85. Maximal Rectangle (JAVA)的更多相关文章
- 85. Maximal Rectangle
85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...
- 刷题85. Maximal Rectangle
一.题目说明 题目,85. Maximal Rectangle,计算只包含1的最大矩阵的面积.难度是Hard! 二.我的解答 看到这个题目,我首先想到的是dp,用dp[i][j]表示第i行第j列元素向 ...
- 求解最大矩形面积 — leetcode 85. Maximal Rectangle
之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...
- 85. Maximal Rectangle (Graph; Stack, DP)
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- 【LeetCode】85. Maximal Rectangle
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
- 【leetcode】85. Maximal Rectangle(单调栈)
Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing onl ...
- LeetCode (85): Maximal Rectangle [含84题分析]
链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...
- LeetCode OJ 85. Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...
- 84. Largest Rectangle in Histogram *HARD* -- 柱状图求最大面积 85. Maximal Rectangle *HARD* -- 求01矩阵中的最大矩形
1. Given n non-negative integers representing the histogram's bar height where the width of each bar ...
随机推荐
- JSP——JSTL定制标签 - 递归标签显示属性结构
编写定制标签分为三个步骤:编写标签处理器.配置标签.使用标签. 1.标签处理器 标签处理器和标签是一一对应的关系 package com.oolong.utils.customtags; impo ...
- pom文件中的dependencyManagement和dependencies的区别
dependencies 子项目中,自动继承父项目中的相关依赖 dependencyManagement 只是声明依赖,并不实现引入,因此子项目中需要显示的声明需要用的依赖.如果不在子项目中声明依赖, ...
- tps抖动
https://blog.csdn.net/lzqinfen/article/details/46820673 tps抖动厉害的原因?突然增加成倍的用户,如果性能表现良好,TPS应该成倍增加,响应时间 ...
- Dubbo Monitor Simple 监控中心
下载项目源码(其中的 dubbo-registry-simple 代表简单的注册中心,用于开发测试,生产环境一般用zookeeper) https://github.com/apache/incuba ...
- spring boot统一异常页面
只需要创建一个类就可以了 package com.ulic.gis.securityManage.controller; import java.util.Map; import javax.serv ...
- DeepFaceLab:手动提取高精度脸图,减少抖动!
DeepFaceLab默认情况下都都是自动提取脸部,整体来说效果不错,脸部曲线识别度也比较高.但是自动不是万能的,有些图片的轮廓识别并不好.而识别不好最直接的结果就是合成的视频可能会出现抖动. 也就是 ...
- Mac下破解百度网盘限速(Chrome + Aria2GUI)
基本原理是利用Aria2GUI的多线程下载来达到提速的目的,具体步骤如下: 1.下载Aria2GUI客户端(注意,客户端文件要放入‘应用程序’,否则会报错),使用时注意修改线程数,默认为16,不够用, ...
- internetwork 与 the Internet的区别
internetwork 表示:互连网络 the Internet 表示:因特网
- VirtualBox上Centos7磁盘扩容
VirtualBox上Centos7磁盘扩容 非常实用 点击直达
- python学习笔记:(十一)模块
模块是指一个包含定义的函数和变量的文件,其后缀名为.py.模块可以被别的程序引用,并使用其中的函数等功能. 1.import语句 如果需要使用模块,只需要在新模块中导入模块.使用import关键字 如 ...