Java for LeetCode 084 Largest Rectangle in Histogram【HARD】
For example,
Given height = [2,1,5,6,2,3]
,
return 10
.
解题思路:
参考Problem H: Largest Rectangle in a Histogram第四种思路,或者翻译版Largest Rectangle in Histogram@LeetCode,JAVA实现如下:
public int largestRectangleArea(int[] height) {
Stack<Integer> stk = new Stack<Integer>();
int ret = 0;
for (int i = 0; i <= height.length; i++) {
int h=0;
if(i<height.length)
h=height[i];
if (stk.isEmpty() || h >= height[stk.peek()])
stk.push(i);
else {
int top = stk.pop();
ret = Math.max(ret, height[top] * (stk.empty() ? i : i - stk.peek() - 1));
i--;
}
}
return ret;
}
Java for LeetCode 084 Largest Rectangle in Histogram【HARD】的更多相关文章
- LeetCode 84. Largest Rectangle in Histogram 单调栈应用
LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...
- leetcode之Largest Rectangle in Histogram
问题来源:Largest Rectangle in Histogram 问题描述:给定一个长度为n的直方图,我们可以在直方图高低不同的长方形之间画一个更大的长方形,求该长方形的最大面积.例如,给定下述 ...
- 关于LeetCode的Largest Rectangle in Histogram的低级解法
在某篇博客见到的Largest Rectangle in Histogram的题目,感觉蛮好玩的,于是想呀想呀,怎么求解呢? 还是先把题目贴上来吧 题目写的很直观,就是找直方图的最大矩形面积,不知道是 ...
- 【LeetCode】084. Largest Rectangle in Histogram
题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...
- [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 ...
- [LeetCode OJ] 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
Problem: Given n non-negative integers representing the histogram's bar height where the width of ea ...
- LeetCode 84. Largest Rectangle in Histogram 直方图里的最大长方形
原题 Given n non-negative integers representing the histogram's bar height where the width of each bar ...
随机推荐
- iOS中后台运行
iOS在升级到4.0以后就支持了多任务了.下文将详细介绍一下这个特性. 1.检查设备是否支持多任务 Apple出于性能的考虑,并不是所有的iOS设备升级到iOS4以后都支持多任务,比如iPhone 3 ...
- VS2010 MFC中 使用CListCtrl的排序功能
list 控件是creat的,不是拖在对话框上的.想使用CListCtrl的排序功能却犯了愁~~~ 还好找到方法,如下: .h文件里:afx_msg void OnLvnColumnclickList ...
- mybatis配置mapperLocations多个路径
<property name="mapperLocations"> <array> <value>classpath*:/mybatis-con ...
- Drools环境搭建
Eclipse3.5安装Drools6.5.0.Final插件 到Drools下载页面(现在是http://www.jboss.org/drools/downloads.html) -下载并解压Dro ...
- 转:java的各个拓展类库的推荐方案
from: 链接:https://www.zhihu.com/question/21142149/answer/109854408 Java是世界上最强大的编程语言之一,很多开发人员和大型企业都偏爱J ...
- 收藏以下linux查看系统信息的命令
# uname -a # 查看内核/操作系统/CPU信息# head -n 1 /etc/issue # 查看操作系统版本# hostname ...
- Node.js自动化测试及大规模性能测试技术实现(Java&Node.JS)
后续计划: 改进1:性能测试Tool由Client端设计成Server端,支持分布式中控部署 改进2:SocketTestFramework集成WebSocket协议 改进3:完善Data Analy ...
- [ACM] HDU 5024 Wang Xifeng's Little Plot (构造,枚举)
Wang Xifeng's Little Plot Problem Description <Dream of the Red Chamber>(also <The Story of ...
- Linux内存段的分析
Linux 应用程序的内存分配中,是用 segment(段)进行区别的,使用 size 命令进行查看: size a.out text data bss dec hex filename a. ...
- C 语言学习 3
[程序3] 题目:一个整数,它加上100后是一个全然平方数.再加上168又是一个全然平方数.请问该数是多少? 1.程序分析:在10万以内推断.先将该数加上100后再开方,再将该数加上268后再开方,假 ...