[leetcode]84. Largest Rectangle in Histogram直方图中的最大矩形
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
题意:
给定一个直方图,求其能覆盖的最大矩形。
思路:
以下是O(n2) 的时间复杂度解法

当 i= 0 1 2 3 4
area= 2*1 4*1 6*1 5*1 3*1
2*2 4*2 5*2 3*2
2*3 4*3 3*3
2*4 3*4
2*5
以height[i]为临界,每次比较其左侧的所有height谁更小,更新minHeight, 更新当前maxArea
class Solution {
public int largestRectangleArea(int[] heights) {
int maxArea = 0;
for(int i = 0; i < heights.length; i++){
int minHeight = heights[i]; //以height[i]为临界
for(int j = i; j >= 0; j--){ // 每次比较其左侧的所有height谁更小
minHeight = Math.min(minHeight, heights[j]);
maxArea = Math.max(maxArea, minHeight * (i - j + 1));
}
}
return maxArea;
}
}
以下是O(n) 的时间复杂度解法
维护一个单调栈monoStack,数组中每个元素的index都入栈、出栈
代码:
class Solution {
public int largestRectangleArea(int[] heights) {
Stack<Integer> s = new Stack<>();
int area = 0;
for(int i=0; i<= heights.length;){
int value = i< heights.length ? heights[i]: 0;
if(s.isEmpty() || value> heights[s.peek()]){
s.push(i);
i++;
}else{
int temp = s.pop();
area = Math.max(area, heights[temp] * (s.isEmpty() ? i: (i-s.peek()-1)));
}
}
return area;
}
}
[leetcode]84. Largest Rectangle in Histogram直方图中的最大矩形的更多相关文章
- [LeetCode] 84. Largest Rectangle in Histogram 直方图中最大的矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- LeetCode 84. Largest Rectangle in Histogram 直方图里的最大长方形
原题 Given n non-negative integers representing the histogram's bar height where the width of each bar ...
- LeetCode 84. Largest Rectangle in Histogram 单调栈应用
LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...
- 【LeetCode】84. Largest Rectangle in Histogram——直方图最大面积
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- leetCode 84.Largest Rectangle in Histogram (最大矩形直方图) 解题思路和方法
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- [leetcode]84.Largest Rectangle in Histogram ,O(n)解法剖析
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- [LeetCode#84]Largest Rectangle in Histogram
Problem: Given n non-negative integers representing the histogram's bar height where the width of ea ...
- 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...
随机推荐
- Hystrix 学习使用
说明: 每次调用创建一个新的HystrixCommand,把依赖调用封装在run()方法中 执行execute()/queue做同步或异步调用 请求接收后,会先看是否存在缓存数据,如果存在,则不会继续 ...
- SQL特殊comment语法
SQL 注释的特殊用法: /*!版本号 语句*/ 表示大于等于某个版本是,才执行相应的语句. 在版本为5.7.23的MySQL上做测试如下: 测试1 mysql> select 1 /*!507 ...
- TreeSet的两种排序方式,含Comparable、Comparator
1.排序的引入 由于TreeSet可以实现对元素按照某种规则进行排序,例如下面的例子 public class TreeSetDemo { public static void main(String ...
- SysUtils.CompareText的注释
两个字符串对象进行比较,忽略大小写,两个字符串缓冲区地址利用EAX和EDX两个寄存器传给该函数,字符串的长度用4个字节保存在缓冲区的前面,函数用EAX返回比较结果,结果为0表示相同. function ...
- excel 设置的函数在打开的时候不会自动执行
excel中设置了个today的函数,显示今天的日期,结果不执行. 解决方案:打开该excel,选择File-->Options ,在弹出的框框中选择Formulas,在主界面的Calcula ...
- [UE4]VR角色形象:Lock to Hmd、Use Pawn Control Rotation
Camera组件是自动跟着头显一起移动的,所以只要给Camera的子控件添加一个Static Mesh或者Skeletal Mesh并选择合适的模型就可以了. 要记得勾选Lock to Hmd(锁定到 ...
- 20165312 实验一 Java开发环境的熟悉
20165312 实验一 Java开发环境的熟悉 一.实验内容及步骤 (一)使用JDK编译.运行简单的Java程序 Ctrl+Shift+T打开终端 输入cd cxgg20165312/test进入目 ...
- 1DAY centos 7.4 u盘安装、网络安装
0xff01 重庆大学开源下载centos 1.下载地址 http://mirrors.cqu.edu.cn/CentOS/7.4.1708/isos/x86_64/ 选择 CentOS-7-x86 ...
- PHP chdir函数:改变当前的目录
PHP chdir函数的作用是改变当前的目录,这里主机吧详细介绍下chdir函数的用法,并列举使用chdir函数的例子. chdir定义和用法: chdir() 函数改变当前的目录. chdir实例: ...
- Using a ScrollView - RN4
使用滚动条. 1. import import {ScrollView} from "react-native"; 2. Using <ScrollView> ... ...