原题地址: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. python之抽象基类

    抽象基类特点 1.不能够实例化 2.在这个基础的类中设定一些抽象的方法,所有继承这个抽象基类的类必须覆盖这个抽象基类里面的方法 思考 既然python中有鸭子类型,为什么还要使用抽象基类? 一是我们在 ...

  2. UVA101 【The Blocks Problem】

    一个大模拟!!! 总的来说就是碰到move就要把a上面的全部放回原处. 如果碰到onto就要把b上面的全部放到原处. 因为move是只移动a一个,所以a上面的要归位,而pile是移一堆,所以不用. o ...

  3. 利用HTML5开发Android

    ● Android设备多分辨率的问题 Android浏览器默认预览模式浏览 会缩小页面 WebView中则会以原始大小显示 Android浏览器和WebView默认为mdpi.hdpi相当于mdpi的 ...

  4. C#--整型与字节数组byte[]之间的转换

    using System; int  i = 123;byte [] intBuff = BitConverter.GetBytes(i);     // 将 int 转换成字节数组lob.Write ...

  5. 《剑指offer》青蛙跳台阶

    题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 很裸的斐波那契数列. class Solution { public: int jumpFloor ...

  6. ERP合同管理二(三十)

    未审核表单列表显示: 1.用户登录后,根据登录用户加载审核流程表中属于当前登录用户的未审核表单.2.点击选中未审核表单跳转到指定审核流程页面 if (Request.QueryString[" ...

  7. Java 和 C++ 的部分区别

    1. Java是解释型语言,所谓的解释型语言,就是源码会先经过一次编译,成为中间码,中间码再被解释器解释成机器码.对于Java而言,中间码就是字节码(.class),而解释器在JVM中内置了. 2. ...

  8. Python subprocess方法

    import subprocess #subprocess.call("df -h",shell=True,stdout=subprocess.PIPE)#打印到视图,但是不能保存 ...

  9. Java Swing 实时刷新JTextArea,以显示不断append的内容?

    方法一: 在代码中执行完textArea.append("message")后,如果你想让这个更新立刻显示在界面上而不是等swing的主线程返回后刷新,我们一般会在该语句后调用te ...

  10. @RequestParam注解使用:Name for argument type [java.lang.String] not available, and parameter name information not found in class file either.

    详细错误信息 Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Re ...