题目来源:

  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. keep健身计划

    下一个月计划 1keep二周计划 2百度第一期学完 3百度前端技术学院提升

  2. mysql关联更新

    update tb_sdd_info a,tb_bnm_evian_info b set a.username=b.username where a.username=b.memberno and  ...

  3. 怎样使用Markdown

    转自:http://wowubuntu.com/markdown/basic.html 段落.标题.区块代码 一个段落是由一个以上的连接的行句组成,而一个以上的空行则会划分出不同的段落(空行的定义是显 ...

  4. 关于Scrapy框架的基本概念

    Scrapy爬取网页基本概念 Scrapy爬取网页基本概念 怎么样用Scrapy生成project? scrapy startproject xxx 如何用Scrapy爬取网页? import scr ...

  5. linux杂记(六)档案权限

    [KANO@kelvin ~]$ ls -al 总用量 drwx------. KANO KANO 10月 : . drwxr-xr-x. root root 9月 : .. drwxrwxr-x. ...

  6. Android Gradle配置

    解决问题 错误: Could not find the AndroidManifest.xml file, going up from path //打开app build.gradle文件加入以下代 ...

  7. System类基础

    取时间差: public class SystemDemo01 {     public static void main(String[] args) {         long startTim ...

  8. Let's Format Css Documents

    每次想参考一些好看网站的时候,打开css文档都是一行的,琢磨了下就自己写了块短短的代码,各路Java大神别笑我呀.^_^ 复制粘贴控制台的输出就好了.(瞬间觉得跟上大神的脚步了←_←) package ...

  9. postman下载和安装

    插件下载地址:http://download.csdn.net/download/zhanghaofor/8244137 下载后解压缩,里面有安装方法 1.找到后缀为crx的文件,将后缀改成rar并解 ...

  10. android中ScrollView和GridView/ListView共存时,ScrollView不在顶部的解决方法

    listView.setFocusable(false); gridView.setFocusable(false); 这个必须在代码中写,xml文件中设置不起作用 原文:http://stackov ...