题目来源:

  https://leetcode.com/problems/largest-rectangle-in-histogram/


题意分析:

  给定一个数组,数组的数字代表这个位置上的bar的高度,在这些bar中找出最大面积的矩阵。例如height = [2,1,5,6,2,3]得到的图是

那么他的最大面积是

所以结果是10.


题目思路:

  这是一道巧妙的算法题。首先,将bar的高度append到stack里,当遇到新的高度的时候就有3种情况,①如果newheight > stack[-1],那么将这个高度append到stack里面;②如果相等,那么忽略;③如果newheight < stack[-1],那么将stack里面的元素pop出来并且记录到这个高度的最大面积,直到stack为空,或者高度大于当前高度。


代码(python):

  

 class Solution(object):
def largestRectangleArea(self, heights):
"""
:type heights: List[int]
:rtype: int
"""
mans = 0
ans,ansindex,i = [],[],0
while i < len(heights):
if len(ans) == 0:
ans.append(heights[i])
ansindex.append(i)
else:
if heights[i] >= ans[-1]:
ans.append(heights[i])
ansindex.append(i)
else:
lastindex = 0
while len(ans) > 0 and ans[-1] > heights[i]:
tmp = ans.pop()
lastindex = ansindex.pop()
mans = max(mans,tmp*(i - lastindex))
ans.append(heights[i])
ansindex.append(lastindex)
i += 1
lastindex = 0
while len(ans) != 0:
tmp = ans.pop()
mans = max(mans,tmp*(len(heights) - ansindex.pop()))
return mans

[LeetCode]题解(python):084-Largest Rectangle in Histogram的更多相关文章

  1. Java for LeetCode 084 Largest Rectangle in Histogram【HARD】

    For example, Given height = [2,1,5,6,2,3], return 10. 解题思路: 参考Problem H: Largest Rectangle in a Hist ...

  2. LeetCode 笔记系列 17 Largest Rectangle in Histogram

    题目: Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar he ...

  3. 【LeetCode】084. Largest Rectangle in Histogram

    题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...

  4. LeetCode(84) Largest Rectangle in Histogram

    题目 Given n non-negative integers representing the histogram’s bar height where the width of each bar ...

  5. 084 Largest Rectangle in Histogram 柱状图中最大的矩形

    给出 n 个非负整数来表示柱状图的各个柱子的高度,每个柱子紧挨彼此,且宽度为 1 .您的函数要能够求出该柱状图中,能勾勒出来的最大矩形的面积. 详见:https://leetcode.com/prob ...

  6. 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...

  7. leetcode之Largest Rectangle in Histogram

    问题来源:Largest Rectangle in Histogram 问题描述:给定一个长度为n的直方图,我们可以在直方图高低不同的长方形之间画一个更大的长方形,求该长方形的最大面积.例如,给定下述 ...

  8. leetcode Largest Rectangle in Histogram 单调栈

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

  9. LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle

    1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ...

  10. LeetCode 84. Largest Rectangle in Histogram 单调栈应用

    LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...

随机推荐

  1. HDOJ 1561 - 树形DP,泛化背包

    刚看题...觉得这不是棵树...可能有回路...仔细一想..这还真是棵树(森林)...这是由于每个城堡所需要提前击破的城堡至多一个..对于一个城堡.其所需提前击破的城堡作为其父亲构图.... dp[k ...

  2. docker private registry使用

    一.搭建harbor: 步骤:略 二.命令行操作: 登录:docker login docker01 tag image: tag 一个 image,名称一定要标准( registryAddress[ ...

  3. c#学习已经停止了

    为了养家糊口,没有时间学习c#.

  4. IIS7.0/7.5 MVC3 实现伪静态

    routes.MapRoute(            "Default",            "{controller}/{action}.html/{id}&qu ...

  5. Java通过JDBC链接数据库,数据库中wen

    连接数据库设置编码 jdbc:mysql://地址:3306/数据库名?characterEncoding=utf8

  6. Android入门——UI(7)——Fragment

    先上fragment静态加载的代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...

  7. linux 分割文件

    import os import sysimport subprocess if len(sys.argv)<3 : print 'usage: filenum filename' file_n ...

  8. WCF编写时候的测试

    1右击WCF创建到使用到发布这篇文章中的类库项目中的接口类实现文件添加断点 2右击WCF创建到使用到发布这篇文章中的WCF服务网站设为启动项并允许 3右击WCF创建到使用到发布这篇文章中的WPF项目调 ...

  9. 几个SQL

    select sum(`value`) from testtable where value != 'error' AND type ='b' in (select DISTINCT(type) fr ...

  10. mysql不区分大小写解决

    今天遇到一个情况,前台验证用户昵称的时候发现无论输入Fred fred亦或是FrEd 都会显示昵称存在(这并不是我所期望的结果) debug发现并不是程序问题 hibernate也只是吧hql装成my ...