[LeetCode]题解(python):084-Largest Rectangle in Histogram
题目来源:
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的更多相关文章
- 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 ...
- LeetCode 笔记系列 17 Largest Rectangle in Histogram
题目: Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar he ...
- 【LeetCode】084. Largest Rectangle in Histogram
题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...
- LeetCode(84) Largest Rectangle in Histogram
题目 Given n non-negative integers representing the histogram’s bar height where the width of each bar ...
- 084 Largest Rectangle in Histogram 柱状图中最大的矩形
给出 n 个非负整数来表示柱状图的各个柱子的高度,每个柱子紧挨彼此,且宽度为 1 .您的函数要能够求出该柱状图中,能勾勒出来的最大矩形的面积. 详见:https://leetcode.com/prob ...
- 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...
- leetcode之Largest Rectangle in Histogram
问题来源:Largest Rectangle in Histogram 问题描述:给定一个长度为n的直方图,我们可以在直方图高低不同的长方形之间画一个更大的长方形,求该长方形的最大面积.例如,给定下述 ...
- leetcode Largest Rectangle in Histogram 单调栈
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...
- 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 ...
- LeetCode 84. Largest Rectangle in Histogram 单调栈应用
LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...
随机推荐
- cocos2d-x创建场景
今天开始学习Cocos2d-x,使用的版本是2.1.4,这个版本比较老,对应的参考资料也比较齐全. 在mac/xcode环境下,代码是写在Classes文件夹下的,和iOS应用类似,程序从AppDel ...
- iOS开发基本须要
iOS开发基本须要 1 准备好对应的硬件和软件配置------基于Intel处理器的Mac操作系统 2 具有C++,Java或其它面向对象编程语言的开发经验 3 最新的iphone SDK的下 ...
- 【Cocos2d TestCpp实例模仿一】-- ActionsTest
转载请注明出处:http://blog.csdn.net/oyangyufu/article/details/25252539 CCActionInterval(持续性动作) 位置性变化动作以To结束 ...
- selector是在文件夹drawable中进行定义的xml文件。
获取Drawable对象: Resources res = mContext.getResources(); Drawable myImage = res.getDrawable(R.drawable ...
- zoj 1025Wooden Sticks(贪心)
递增子序列的最小组数.可以直接贪心,扫一遍 #include<iostream> #include<cstring> #include<cstdio> #inclu ...
- localhost简介、localhost与 127.0.0.1 及 本机IP 的区别
localhost是什么意思? 相信有人会说是本地ip,曾有人说,用127.0.0.1比localhost好,可以减少一次解析. 看来这个入门问题还有人不清楚,其实这两者是有区别的. localhos ...
- Java 内省机制
一.内省 内省(Introspector) 是Java 语言对 JavaBean 类属性.事件的一种缺省处理方法.JavaBean是一种特殊的类,主要用于传递数据信息,这种类中的方法主要用于访问私有的 ...
- POJ1323-Game Prediction
描述: Suppose there are M people, including you, playing a special card game. At the beginning, each p ...
- mahout算法源码分析之Collaborative Filtering with ALS-WR 并行思路
Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit. mahout算法源码分析之Collaborative Filtering with ALS-WR 这个算 ...
- Django Web开发【4】 用户注册与管理
几乎所有的网站都提供了用户注册与管理功能,这一节,我们将讲解如何利用Django自身提供的用户认证系统实现用户注册与管理功能. 会话认证 在上一节中,我们学习了User数据模型,并用它来保存用户信息, ...