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)的更多相关文章

  1. leetcode Largest Rectangle in Histogram 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...

  2. [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  3. LeetCode: Largest Rectangle in Histogram 解题报告

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  4. [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  5. LeetCode: Largest Rectangle in Histogram(直方图最大面积)

    http://blog.csdn.net/abcbc/article/details/8943485 具体的题目描述为: Given n non-negative integers represent ...

  6. [leetcode]Largest Rectangle in Histogram @ Python

    原题地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/ 题意: Given n non-negative integ ...

  7. [LeetCode] Largest Rectangle in Histogram

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  8. [LeetCode] Largest Rectangle in Histogram 解题思路

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  9. LeetCode——Largest Rectangle in Histogram

    Question Given n non-negative integers representing the histogram's bar height where the width of ea ...

随机推荐

  1. 动态执行linq 语句 NLinq

    using Evaluant.NLinq.Memory;using System.Collections.Generic;using Evaluant.NLinq;using System.Colle ...

  2. andriod的简单用法1

    1.从一个Activity跳转到另一个Activity,使用Intent. 在按钮的onClick中如下写法: public void Login(View view) { Intent intent ...

  3. Spring学习笔记(一) Spring基础IOC、AOP

    1.       注入类型 a)       Spring_0300_IOC_Injection_Type b)       setter(重要) c)       构造方法(可以忘记) d)     ...

  4. 一站式Hadoop&Spark云计算分布式大数据和Android&HTML5移动互联网解决方案课程(Hadoop、Spark、Android、HTML5)V2的第一门课程

    Hadoop是云计算的事实标准软件框架,是云计算理念.机制和商业化的具体实现,是整个云计算技术学习中公认的核心和最具有价值内容. 如何从企业级开发实战的角度开始,在实际企业级动手操作中深入浅出并循序渐 ...

  5. maven 的各种命令

    mvn clean : 清理旧的文件 mvn clean compile :  清理 .编译 mvn clean test :       清理 .编译 .测试 mvn clean package : ...

  6. Java设计模式系列之桥接模式

    桥接模式(Bridge)的定义 在软件系统中,某些类型由于自身的逻辑,它具有两个或多个维度的变化,那么如何应对这种“多维度的变化”?这就要使用桥接模式 将抽象部分与它的实现部分分离,使它们都可以独立地 ...

  7. stm32f407 定时器 用的APB1 APB2 及 定时器频率

    上午想要用Timer10做相对精确的延时功能,但是用示波器发现实际延时数值总是只有一半,百思不得其解.仔细查阅各处资料结合实际研究后对stm32f407的14个定时器的时钟做一个总结: 下面来源: h ...

  8. POJ1189钉子和小球(DP)

    对钉子DP,如果钉子存在DP[i+1][j]+=DP[i][j]; DP[i+1][j+1]+=DP[i][j]; 如果不存在DP[i+2][j+1]+=4*DP[i][j]; 见代码:(有一个比较坑 ...

  9. JS escape、encodeURI 、encodeURIComponent 编码与解码[转]

    转至:http://jc-dreaming.iteye.com/blog/1702407 本文讨论如何对传递参数用JS编码与解码 1:编码与解码方法的对应关系 escape ------------- ...

  10. Django 使用原生SQL

    def dictfetchall(cursor): "将游标返回的结果保存到一个字典对象中" desc = cursor.description return [ dict(zip ...