84. Largest Rectangle in Histogram(直方图最大面积 hard)
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.
For example,
Given heights = [2,1,5,6,2,3],
return 10.
用桟
class Solution {
public int largestRectangleArea(int[] heights) {
Stack<Integer> stack = new Stack<Integer>();
//array 扩容,在最后一位加上0
int[] a = new int[heights.length+1];
for(int i =0;i<heights.length;i++)
a[i] = heights[i];
a[heights.length]=0;
//
int answer = 0;
int temp;
for(int i = 0;i<a.length;){
if(stack.isEmpty()||a[i]>a[stack.peek()]){//a[i]与桟顶元素比较
stack.push(i);
i++;
}
else{
temp = stack.pop();
answer = Math.max(answer,a[temp]*(stack.isEmpty()?i: i-stack.peek()-1));
//桟为空的时候,需要计算长度为i 高度从0到i最矮的(即桟中最小的元素,弹出后,桟就空了)
}
}
return answer;
}
}
84. Largest Rectangle in Histogram(直方图最大面积 hard)的更多相关文章
- 【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(直方图最大面积)
http://blog.csdn.net/abcbc/article/details/8943485 具体的题目描述为: Given n non-negative integers represent ...
- [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 ...
- [leetcode]84. Largest Rectangle in Histogram直方图中的最大矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- 刷题84. Largest Rectangle in Histogram
一.题目说明 题目84. Largest Rectangle in Histogram,给定n个非负整数(每个柱子宽度为1)形成柱状图,求该图的最大面积.题目难度是Hard! 二.我的解答 这是一个 ...
- 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...
- 84. Largest Rectangle in Histogram
https://www.cnblogs.com/grandyang/p/4322653.html 1.存储一个单调递增的栈 2.如果你不加一个0进去,[1]这种情况就会输出结果0,而不是1 3.单调递 ...
- LeetCode 84. Largest Rectangle in Histogram 单调栈应用
LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...
- 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 ...
随机推荐
- error: Please reinstall the libcurl distribution - easy.h should be in <curl-dir>/include/curl/
运行php-5.3.10 --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --e ...
- querySelectorAll 和getElementsByClassName的区别
querySelectorAll 返回的是映射 改变其值不会改变document 而getElementsByClassName 改变它就会改变document 摘自JavaScript权威指南(jQ ...
- IE8 frameset SESSION丢失
IE8 使用 frameset后,子页面,脚本触发父页面js函数, 如果使用 window.parent.location.href 跳转,则session会丢失 使用 window.location ...
- PHP mysql基本语句指令
/*选择数据库 use test; */ /* 显示所有的数据库 show databases; */ /*删除表/数据库 drop database test1; delete from user1 ...
- angular4 在页面跳转的时候传递多个参数到新页面
页面跳转 router.navigate //单一参数: this.router.navigate(['/detail',id]); //多个参数: this.router.navigate(['/d ...
- warning: Now you can provide attr "wx:key" for a "wx:for" to improve performance.
小程序开发过程中在写for循环的时候会出现如下报错 warning: Now you can provide attr "wx:key" for a "wx:for&qu ...
- Openstack实现共有云多flat网络
首先给两台虚拟机添加网卡,模式为仅主机模式 配置控制节点IP /etc/sysconfig/network-scripts/ifcfg-eth1 TYPE=Ethernet BOOTPROTO=sta ...
- c# 解析json 字符串 报异常 Bad JSON escape sequence 解决方案
当我试图将一个完整的本地路径的字符串串(如:c:\\aaa\\数学题\\三一班\\ea15ae66-d5cd-4244-87e4-fcf97b06b407.jpg)encodeURL之后当做一个页面参 ...
- Go语言 基本类型
在内存中的形式 首先看一下在go中,一些基础类型在内存中是以什么形态存在的,如下图所示: 变量j的类型是int32, 而变量i的类型是int,两者不是同一个类型,所以赋值操作i=j是一种类型错误can ...
- 关于spring的applicationContext.xml配置文件的ref和value之自我想法
今天在做SSH的一个项目的时候,因为需要定时操作,所以就再sping里面加入了一个quartz的小定时框架,结果在运行时候,发生了一个小bug. Caused by: org.springframew ...