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 ...
随机推荐
- [原创]Android应用 - YE启动器APP(YeLauncherApp)
支持解冻.冻结APP 支持在本APP列表隐藏指定APP 支持检测系统核心APP,防止误点冻结导致手机变砖 支持清理APP缓存 支持强制停止APP Apk文件仅仅47KB 自用.开源 类似APP:A ...
- 将webkit内核封装为duilib的浏览器控件
转载请说明出处,谢谢~~ 原本的duilib是自带浏览器控件的,但是使用了IE内核,我在做仿酷狗音乐播放器时,在右侧乐库要用到浏览器控件,而我使用自带的IE控件却发现了不少缺点,这也是duilib一直 ...
- 通过反汇编C语言小程序学习Liunx汇编语言
大家好! 我是来自山东师范大学的吴乐. 今天在<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ...
- 使用PPA在ubuntu上安装emacs
使用PPA(Personal Package Archive)在ubuntu上安装emacs 1添加 PPA 到 apt repository 中: $ sudo add-apt-reposito ...
- MySQL CURDATE() 函数
定义和用法 CURDATE() 函数返回当前的日期. 语法 CURDATE() 实例 例子 1 下面是 SELECT 语句: SELECT NOW(),CURDATE(),CURTIME() 结果类似 ...
- BootStrap入门教程 (一) :手脚架Scaffolding(全局样式(Global Style),格网系统(Grid System),流式格网(Fluid grid System),自定义(Customing),布局(Layouts))
2011年,twitter的“一小撮”工程师为了提高他们内部的分析和管理能力,用业余时间为他们的产品构建了一套易用.优雅.灵活.可扩展的前端工具集--BootStrap.Bootstrap由MARK ...
- RabbitMQ (四) 路由选择 (Routing) -摘自网络
本篇博客我们准备给日志系统添加新的特性,让日志接收者能够订阅部分消息.例如,我们可以仅仅将致命的错误写入日志文件,然而仍然在控制面板上打印出所有的其他类型的日志消息. 1.绑定(Bindings) 在 ...
- http响应Last-Modified和ETag(转)
基础知识 1) 什么是”Last-Modified”? 在浏览器第一次请求某一个URL时,服务器端的返回状态会是200,内容是你请求的资源,同时有一个Last-Modified的属性标记此文件在服务期 ...
- #JAVA操作LDAP
package com.wisdombud.unicom.monitor.ldap; import java.util.ArrayList; import org.slf4j.Logger; impo ...
- 使用C#读取XML节点,修改XML节点
例子: <add key="IsEmptyDGAddRootText" value="" /> <!--是否可以修改归档状态档案 false: ...