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 ...
随机推荐
- Protobuf C#
// ProtoBuf序列化 using(var file = System.IO.File.Create("Person.bin")) ...
- 【前端GUI】—— 网站美工必须掌握的PS知识点&思维导图
前言:前端离不开与设计的沟通,有时候还需要自己上手改动甚至设计网页,所以这里简单梳理一下近期学习的“网站美工”相关知识及练习.(工作用不上的时候,自己玩儿着也蛮有意思的,哈哈(*゚∀゚*)~) 一.P ...
- iWatch报错: Missing com.apple.developer.healthkit entitlement
今天开发iWatch项目,报错: Optional (Error "Missing come.apple.developer.healthkit entitlement.") Us ...
- mongodb配置副本集(多台服务器间的副本集搭建) replica[ˈrɛplɪkə]
副本集具有多个副本保证了容错性,就算一个副本挂掉了还有很多副本存在,并且解决了“主节点挂掉了,整个集群内会自动切换”的问题.我们来看看mongoDB副本集的架构图: 由图可以看到客户端连接到整个副本集 ...
- 【Python】删除字符串的空白
在程序中,额外的空白可能让人迷惑,对于程序员来说,'python'跟'python '看起来几乎一样,但是对于程序来说,可是千差万别 (lstrip)删除开头空白 >>> Langu ...
- NodeJS 安装cnpm命令行工具
在安装之前,请确保已安装Git和NodeJS. cmd机内命令窗口,输入以下命令: git config --system http.sslcainfo /bin/curl-ca-bundle.crt ...
- 提升Web性能的技巧
1. 采用反向代理服务器(Reverse Proxy Server)来对应用进行加速和保护 其作用主要在以下三方面: 负载平衡 – 运行在反向代理服务器上的负载平衡器会在不同的不服务器 间进行传输 ...
- android开发系列之视频断点续传
今天在这篇博客里面,我想说说自己在这几天遇到的一个棘手的问题,就是视频断点续传的问题.其实这在我们开发中是一个很常见的应用场景,比如视频.音频.pdf等相关的文档.如果之前没有接触过的话,你也许会被这 ...
- Python中strip方法的妙用
[开胃小菜] 当提到python中strip方法,想必凡接触过python的同行都知道它主要用来切除空格.有下面两种方法来实现. 方法一:用内置函数 #<python> if __name ...
- Django之信息聚合
feeds.py #coding:utf-8 __author__ = 'similarface' from django.contrib.syndication.views import Feed ...