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 ...
随机推荐
- iphone/iOS 访问本地数据库sqlite3
Phone也支持访问本地数据库Sqlite 3.这里简单的介绍一下iPhone上Sqlite 3的使用方法. 首先需要在项目中引用Sqlite 3的开发包,下面是在iPhone SDK 3.0下的目录 ...
- Mapxtreme 在64位系统运行网站,提示未能加载文件或程序集,或它的某一个依赖项
在32位系统上开发的网站,现在需要布署到64位系统上运行,布署好后访问提示提示未能加载文件或程序集,或它的某一个依赖项.在网上搜索后,发现是64位下引用dll出现的这个问题.这个问题通常出在引用第三方 ...
- 列表pagesize修改每页显示的数量失效
◇系统错误修复工具 >> 检测微表正确性 原因是删除一些数据导致记录与实际数据不符 转自:http://bbs.dedecms.com/269491.html
- uiaotumator ui測试 高速调试
1. uiaotumator ui測试 Demo.java package uiautomatorDemo1; import java.io.File; import android.graphics ...
- windows 屏幕坐标 窗口坐标 客户区坐标 逻辑坐标 设备坐标之间的关系及转换
设置坐标映射 (1)Windows坐标系统 Windows坐标系分为逻辑坐标系和设备坐标系两种,GDI支持这两种坐标系.一般而言, GDI的文本和图形输出函数使用逻辑坐标,而在客户区移动或按下鼠 ...
- ClassPathXmlApplicationContext和FileSystemXmlApplicationContext区别
ClassPathXmlApplicationContext 默认文件路径是src下那一级classpath:和classpath*:的区别: classpath: 只能加载一个配置文件,如果配置了多 ...
- unity3D中使用Socket进行数据通信(一)
公司今年3D产品的工作中心主要集中在提高产品深度上,通过对竞争产品的分析,发现我们的缺陷在于多人在线与后台管理部分,多人在线使用unity自带的Network能够搞定,后台部分前段时间主要研究了下Sq ...
- 【SQL】SQL Server中存储过程的调试方法
1.以管理员用户登录DB服务器,把域用户追加到「Administrators」组. 2.在本机上以域用户登录,启动VS. 3.追加DB连接 4.右击要debug的存储过程,选择「ストアドプロシージャに ...
- Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental……
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to th ...
- 《android 1: 创建一个安卓项目》
创建方式有两种: 通过Eclipse创建 在工具栏上选择New>android>android application project,或者在导航栏上选择file>new>pr ...