leetcode -- Largest Rectangle in Histogram TODO O(N)
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 height = [2,1,5,6,2,3],
return 10.
本题思路和Trapping Rain Water差不多,计算每个idx的left bound 和right bound(两个bound都需大于height[idx])
时间复杂度为:O(n^2)
public int largestRectangleArea(int[] height) {
// Start typing your Java solution below
// DO NOT write main() function
int len = height.length;
int[] area = new int[len];
for(int i = 0; i < len; i++){
int h = height[i];
int m = i - 1;
for(; m >= 0; m --){
if(height[m] < h){
break;
}
}
m ++;
int n = i + 1;
for(; n < len; n ++){
if(height[n] < h){
break;
}
}
n --;
int width = (n - m) + 1;
area[i] = h * width;
}
int max = 0;
for(int i = 0; i < len; i++){
if(area[i] > max){
max = area[i];
}
}
return max;
}
可以过小数据,大数据挂在输入[1,1,1,1,1,1........] ,说明有很多重复计算,做了一个简单改进,当前高度与上一个相同时,直接将area[i] = area[i-1](line 12-15)
public int largestRectangleArea(int[] height) {
// Start typing your Java solution below
// DO NOT write main() function
int len = height.length;
int[] area = new int[len];
for(int i = 0; i < len; i++){
int h = height[i];
12 if(i >= 1 && h == height[i - 1]){
13 area[i] = area[i - 1];
14 continue;
15 }
int m = i - 1;
for(; m >= 0; m --){
if(height[m] < h){
break;
}
}
m ++;
int n = i + 1;
for(; n < len; n ++){
if(height[n] < h){
break;
}
}
n --;
int width = (n - m) + 1;
area[i] = h * width;
}
int max = 0;
for(int i = 0; i < len; i++){
if(area[i] > max){
max = area[i];
}
}
return max;
}
leetcode -- Largest Rectangle in Histogram TODO O(N)的更多相关文章
- leetcode Largest Rectangle in Histogram 单调栈
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...
- [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- LeetCode: Largest Rectangle in Histogram 解题报告
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- [LeetCode] 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]Largest Rectangle in Histogram @ Python
原题地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/ 题意: Given n non-negative integ ...
- [LeetCode] 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 bar is ...
- LeetCode——Largest Rectangle in Histogram
Question Given n non-negative integers representing the histogram's bar height where the width of ea ...
随机推荐
- HDU 4336-Card Collector(状压,概率dp)
题意: 有n种卡片,每包面里面,可能有一张卡片或没有,已知每种卡片在面里出现的概率,求获得n种卡片,需要吃面的包数的期望 分析: n很小,用状压,以前做状压时做过这道题,但概率怎么推的不清楚,现在看来 ...
- 五指CMS 3.0 手动升级方法
- markdown常用html标签
换行 测试<br />一下 效果:测试一下 标记 <mark>测试一下</mark> 效果:测试一下 空格 测试一下 效果: 测试一下 ...
- Java基础题
问题: 1.请对比一下重载和重写的区别. 2.请对比一下接口和抽象类的异同. 3.写出一个单例模式,并说明其优点. 4.用过String.StringBuffer吗,说出他们的异同. 5.什么是值传递 ...
- leetcode@ [352] Data Stream as Disjoint Intervals (Binary Search & TreeSet)
https://leetcode.com/problems/data-stream-as-disjoint-intervals/ Given a data stream input of non-ne ...
- 32位和64位dll判断
如何判断一个dll文件是32位还是64位? 1. 开发中经常会使用到VC的一个工具 Dependency Walker用depends.exe打开dll,文件名前有64标示的即为64位. 但是这个方式 ...
- (转)UML实践----用例图、类图、对象图、顺序图、协作图、状态图、活动图、组件图、配置图
面向对象的问题的处理的关键是建模问题.建模可以把在复杂世界的许多重要的细节给抽象出.许多建模工具封装了UML(也就是Unified Modeling Language™),这篇课程的目的是展示出UML ...
- jinfo用法
jinfo(Configuration Info for Java)的作用是实时地查看和调整虚拟机各项参数.使用jps命令的-v参数可以查看虚拟机启动时显式指定的参数列表,但如果想知道未被显式指定的参 ...
- 咏南CS多层插件式开发框架支持最新的DELPHI XE7
DATASNAP中间件: 中间件已经在好几个实际项目中应用,长时间运行异常稳定,可无人值守: 可编译环境:DELPHI XE5~DELPHI XE7,无需变动代码: 支持传统TCP/IP方式也支持RE ...
- String中的Indexof,LastIndexOf, Indexofany,LastIndexOfAny 的区别
本文转载自 http://www.cnblogs.com/qinying/archive/2008/09/22/1295730.html 定位子串是指在一个字符串中寻找其中包含的子串或者某个字符.在S ...