题目来源:

  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. The Suspects(并查集求节点数)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 28164   Accepted: 13718 De ...

  2. angular的那些事

    angular.js是什么 AngularJS 是一个 JavaScript 框架.它可通过 <script> 标签添加到 HTML 页面. AngularJS 通过 指令 扩展了 HTM ...

  3. HTML系列(七):多媒体

    一.video标签 H5新增了video实现在线播放视频的功能: 代码示例: <video controls="controls"> <source src=&q ...

  4. 使用Spire PDF for .NET将HTML转换成PDF文档

    目录 开发环境说明 Spire PDF for .NET (free edition)体验 资源下载 开发环境说明 Microsoft Visual Studio 2013 Ultimate Edit ...

  5. Oracle树反向查询的优化(转载)

    本文系转载,http://technology.amis.nl/2005/08/11/selecting-a-pruned-tree-with-selected-nodes-and-all-their ...

  6. SqlDataAdapter.Update批量数据更新

    SqlDataAdapter.Update批量数据更新 使用SqlDataAdapter.Update可以方便地对数据库进行快速.批量数据更新.我们最常用的多条数据更新方法是使用循环多次执行SQL语句 ...

  7. org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException: Internal Server Error 错误

    Solr报错: { "responseHeader": { "status": 500, "QTime": 11 }, "erro ...

  8. USACO Section 4.3 Street Race(图的连通性+枚举)

    虽说是IOI'95,但是也是挺水的..for 第一问,n最大为50,所以可以直接枚举起点和终点之外的所有点,然后dfs判断是否连通:for 第二问,易知答案一定是第一问的子集,所以从第一问中的答案中枚 ...

  9. 第一个Hadoop程序——Hello Hadoop

    本人原创,转载请注明出处:http://blog.csdn.net/panjunbiao/article/details/12773163 下载Hadoop程序包,下载地址:http://hadoop ...

  10. LineCalc,一个基于Lex&Yacc的简单行计算工具

    LineCalc是基于Lex&Yacc的一个简单的行计算工具,支持常见的运算符和部分POSIX中定义于math.h中的数学函数:同时,LineCalc还提供了一个简单的错误处理模块,能检测公式 ...