原题地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/

题意:

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.

解题思路:又是一道很巧妙的算法题。

Actually, we can decrease the complexity by using stack to keep track of the height and start indexes. Compare the current height with previous one.

Case 1: current > previous (top of height stack)
Push current height and index as candidate rectangle start position.

Case 2: current = previous
Ignore.

Case 3: current < previous
Need keep popping out previous heights, and compute the candidate rectangle with height and width (current index - previous index). Push the height and index to stacks.

(Note: it is better use another different example to walk through the steps, and you will understand it better).

代码:

class Solution:
# @param height, a list of integer
# @return an integer
# @good solution!
def largestRectangleArea(self, height):
maxArea = 0
stackHeight = []
stackIndex = []
for i in range(len(height)):
if stackHeight == [] or height[i] > stackHeight[len(stackHeight)-1]:
stackHeight.append(height[i]); stackIndex.append(i)
elif height[i] < stackHeight[len(stackHeight)-1]:
lastIndex = 0
while stackHeight and height[i] < stackHeight[len(stackHeight)-1]:
lastIndex = stackIndex.pop()
tempArea = stackHeight.pop() * (i-lastIndex)
if maxArea < tempArea: maxArea = tempArea
stackHeight.append(height[i]); stackIndex.append(lastIndex)
while stackHeight:
tempArea = stackHeight.pop() * (len(height) - stackIndex.pop())
if tempArea > maxArea:
maxArea = tempArea
return maxArea

代码:

class Solution:
# @param height, a list of integer
# @return an integer
# @good solution!
def largestRectangleArea(self, height):
stack=[]; i=0; area=0
while i<len(height):
if stack==[] or height[i]>height[stack[len(stack)-1]]:
stack.append(i)
else:
curr=stack.pop()
width=i if stack==[] else i-stack[len(stack)-1]-1
area=max(area,width*height[curr])
i-=1
i+=1
while stack!=[]:
curr=stack.pop()
width=i if stack==[] else len(height)-stack[len(stack)-1]-1
area=max(area,width*height[curr])
return area

常规解法,所有的面积都算一遍,时间复杂度O(N^2)。不过会TLE。

代码:

class Solution:
# @param height, a list of integer
# @return an integer
# @good solution!
def largestRectangleArea(self, height):
maxarea=0
for i in range(len(height)):
min = height[i]
for j in range(i, len(height)):
if height[j] < min: min = height[j]
if min*(j-i+1) > maxarea: maxarea = min*(j-i+1)
return maxarea

[leetcode]Largest Rectangle in Histogram @ Python的更多相关文章

  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

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

  7. 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 ...

  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. eclipse安装主题插件(Color Theme)

    点击“Help”--> Install New Software Name:ColorThemeLocation:http://eclipse-color-theme.github.io/upd ...

  2. InnoDB的关键特性-插入缓存,两次写,自适应hash索引

    InnoDB存储引擎的关键特性包括插入缓冲.两次写(double write).自适应哈希索引(adaptive hash index).这些特性为InnoDB存储引擎带来了更好的性能和更高的可靠性. ...

  3. C++ code:char pointers and char arrays(字符指针与字符数组)

    C-串的正确赋值.复制.修改.比较.连接等方式. #include<iostream> #pragma warning(disable: 4996)//这一句是为了解决“strrev”出现 ...

  4. 视觉显著性检测(Visual saliency detection)相关概念

    视觉显著性检测(Visual saliency detection)指通过智能算法模拟人的视觉特点,提取图像中的显著区域(即人类感兴趣的区域). 视觉注意机制(Visual Attention Mec ...

  5. python 全栈开发,Day21(抽象类,接口类,多态,鸭子类型)

    一.昨日复习 派生方法和派生属性 super 只有在子父类拥有同名方法的时候, 想使用子类的对象调用父类的方法时,才使用super super在类内 : super().方法名(arg1,..) 指名 ...

  6. charAt和String的用法

    package charpter2; import java.util.Scanner; public class Test { public static void main(String[] ar ...

  7. this和引用变量的地址值是同一个---------new后面的是构造方法

    /*全局变量和构造方法及this和引用变量的关系 引用变量在栈里面对象在堆中this你可以理解为在堆里面this就是这个对象自己  */ public class Person {/* * 定义属性 ...

  8. jvm类加载器以及双亲委派

    首先来了解几个概念: 类加载: 概念:虚拟机把描述类的数据从Class文件加载到内存,并对数据进行校验--转换解析--初始化,最终形成能被java虚拟机直接使用的java类型,就是jvm的类加载机制. ...

  9. hihocoder 1343 : Stable Members【拓扑排序】

    hihocoder #1343:题目 解释:一个学习小组,一共有N个学员,一个主管.每个学员都有自己的导师(一个或者多个),导师可以是其他学员也可以是主管.每周学员都要把自己的学习报告和收到的报告提交 ...

  10. 小程序的POST接收不到参数

    这个文档上没有详细说明 在用POST的时候 header要这样写... method:"POST", header: { "Content-Type": &qu ...