[leetcode]Largest Rectangle in Histogram @ Python
原题地址: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的更多相关文章
- leetcode Largest Rectangle in Histogram 单调栈
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...
- [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- LeetCode: Largest Rectangle in Histogram 解题报告
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- LeetCode: Largest Rectangle in Histogram(直方图最大面积)
http://blog.csdn.net/abcbc/article/details/8943485 具体的题目描述为: Given n non-negative integers represent ...
- [LeetCode] Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- 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 ...
- [LeetCode] Largest Rectangle in Histogram 解题思路
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- LeetCode——Largest Rectangle in Histogram
Question Given n non-negative integers representing the histogram's bar height where the width of ea ...
随机推荐
- Laravel Form 表单的数据校验
例如,要使用手机号加验证码的方式提供登录网站的功能,那么在处理前端提交的 form 表单时,就不得不对提交的手机号及验证码做基本的数据校验. 手写规则,非常浪费时间.使用 laravel 内置的 va ...
- STL整理之map
转载请注明出处,部分内容引自李煜东<算法竞赛进阶指南> 前置知识: C++.C语言入门 Map是什么 Map是从键(key)到值(value)的映射,其内部实现是一棵以key为关键码 ...
- bootstrap 列表--水平定义列表
水平定义列表就像内联列表一样,Bootstrap可以给<dl>添加类名“.dl-horizontal”给定义列表实现水平显示效果. @media (min-width: 768px) { ...
- C#泛型(一)
简介: 先看看泛型的概念--“通过参数化类型来实现在同一份代码上操作多种数据类型.利用“参数化类型”将类型抽象化,从而实现灵活的复用”. 很多初学者在刚开始接触泛型的时候会比较难理解 “泛型” 在这里 ...
- Word Highlight设置详解
- jq:翻页时,保存上页多选框checkbox选中状态
这里主要讲一种:中间的 checkbox 是 通过Ajax调出的. 则翻页时,为了保存上页的选定状态,可在页面中定义一个变量,用来存储选中状态的值. <input class="cli ...
- thinkphp错误提示:系统发生错误
下载最新版本3.1.3,定义了一个应用,进入应用的config.php,在里面添加数据库类链接信息,在控制器里面M()一个表,访问控制器方法提示:系统发生错误.如果使用连接字符串DSN方式,调用M() ...
- hdu 1596 乘积的最大值
一般题是 和的最小值 这个是乘积的最大值 Sample Input31 0.5 0.50.5 1 0.40.5 0.4 131 2 //起点 终点2 31 3 Sample Output0.5000. ...
- 三步解决阿里云绑定公网IP地址失败解决方案
1.客户端设置为阿里云服务器的公有地址: 2.服务端设置为阿里云服务器的私有地址: 3.设置阿里云的管理规则: 第一步 第二步 第三.四步
- 第三章XML简介
概念:XML:提供数据交换.系统配置.内容管理等的功能,可跨平台.跨网络.跨程序的数据描述方式.XSL:依靠XPath定位,提供显示模板,且专门为了显示XML文件信息的语言.CSS(层叠样式表):在网 ...