原题地址: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. keras2.0的一些变化

    keras 变化太快了https://github.com/fchollet/keras/wiki/Keras-2.0-release-notes

  2. Keepalived详解之 - LVS(IPVS)管理工具ipvsadm使用指南

    ipvsadm是什么? ipvsadm是用来配置.维护或者查看Linux内核当中virtual server table的一个工具, LVS(Linux virtual server)能基于一个集群当 ...

  3. MACE(2)-----模型编译

    作者:十岁的小男孩 QQ:929994365 无用 本文仅用于学习研究,非商业用途,欢迎大家指出错误一起学习,文章内容翻译自 MACE 官方手册,记录本人阅读与开发过程,力求不失原意,但推荐阅读原文. ...

  4. samba 设置文件的读写权限

    原文:https://blog.csdn.net/lan120576664/article/details/50396511 打开配置文件 sudo pico /etc/samba/smb.conf ...

  5. uva11983扫描线k次覆盖

    自己做的是从下往上扫描的,一直wa,不知道坑在哪里..但是作为模板.我还是找了份不错的ac代码 /* 被覆盖不低于k次的点 每个点对应了一个单位面积,本题把点转面积即是被覆盖不低于k次的面积 可以当做 ...

  6. MVC开发中的常见错误-03-System.Data.Entity.Validation.DbEntityValidationException: 对一个或多个实体的验证失败。有关详细信息,请参见“EntityValidationErrors”属性。

    return Db.SaveChanges()>0; return CurrentDBSession.SaveChanges(); RoleInfoService.EditEntity(role ...

  7. [SDOI2014]数数

    题解: 做过ac自动机上dp的这题应该就很容易想到了 首先在ac自动机上搞dp 表示当前考虑了i位,在自动机的j位上 然后转移就可以了 考虑限制 显然是一个数位dp 考虑位数小于n显然满足要求 考虑位 ...

  8. P3331 [ZJOI2011]礼物(GIFT)

    题解: 首先转化为平面问题 对于每一个z,f(x,y)的值为它能向上延伸的最大高度 ...莫名其妙想出来的是n^4 以每个点作为右下边界n^3枚举再o(n)枚举左下边界计算z的最大值 然而很显然这种做 ...

  9. 6-2 S树 uva712

    这题关键是  反转    查询是固定按照x1x2x3来的   那么先收集前面的顺序  然后在数组里面直接调用即可 比如前面的树是 x3 x1 x2  就把这个当作数组下标 最左边的树是 1<&l ...

  10. 8. 博客系统| 富文本编辑框和基于bs4模块防御xss攻击

    views.py @login_required def cn_backend(request): article_list = models.Article.objects.filter(user= ...