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 ...
随机推荐
- MFC存储图片到SQL Server数据库
第一步:建立数据库表,比如:id char,pic image. 第二步:建立MFC单文档应用程序,再添加类CMyRecordset,基类选择CRecordset,导入数据库的刚建立的表. 第三步:在 ...
- [转]ASP.NET MVC 5 - 控制器
MVC代表: 模型-视图-控制器 .MVC是一个架构良好并且易于测试和易于维护的开发模式.基于MVC模式的应用程序包含: · Models: 表示该应用程序的数据并使用验证逻辑来强制实施业务规则的数据 ...
- Redis异构集群数据在线迁移工具Redis-Migrate-Tool【转】
摘要:Redis-Migrate-Tool(后面都简称RMT),是唯品会开源的redis数据迁移工具,主要用于异构redis集群间的数据在线迁移,即数据迁移过程中源集群仍可以正常接受业务读写请求,无业 ...
- 部署全局ajax处理
$.ajaxSetup({ beforeSend:function(){ $('.loading').show(); }, complete:function(){ $('.loading').fad ...
- ios浅谈关于nil和 NIL区别及相关问题
本文转载至:http://blog.csdn.net/guozh/article/details/8469131 1.nil和null从字面意思来理解比较简单,nil是一个对象,而NULL是一个值,我 ...
- Android Design Support Library 中控件的使用简单介绍(一)
Android Design Support Library 中控件的使用简单介绍(一) 介绍 在这个 Lib 中主要包含了 8 个新的 material design 组件!最低支持 Android ...
- [Android] 开源框架 Volley 自定义 Request
今天在看Volley demo (https://github.com/smanikandan14/Volley-demo), 发现自定义GsonRequest那块代码不全, 在这里贴一个全的. pu ...
- java如何发起一次http的post请求?
@RequestMapping(value = "loginInSSO", method = RequestMethod.GET) public String loginInSSO ...
- 一些VS2013的使用技巧(转载)
1. Peek View 可以在不新建TAB的情况下快速查看.编辑一个函数的代码. 用法:在光标移至某个函数下,按下alt+F12. 然后在Peek窗口里可以继续按alt+F12.然后按ctrl+al ...
- spring配置文件注解方式引入的两种方式
一.#{beanID['propertiesName']}方式 <bean id="propertyConfigurer" class="org.springfra ...