[LC] 84. 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.
Example:
Input: [2,1,5,6,2,3]
Output: 10 Solution 1: O(N^2) LTE
class Solution(object):
def largestRectangleArea(self, heights):
"""
:type heights: List[int]
:rtype: int
"""
if not heights:
return 0
res = -1
for i, height in enumerate(heights):
min_height = height
res = max(res, min_height)
for j in range(i + 1, len(heights)):
min_height = min(min_height, heights[j])
res = max(res, (j - i + 1) * min_height)
return res
Solution 2: O(N)
class Solution(object):
def largestRectangleArea(self, heights):
"""
:type heights: List[int]
:rtype: int
"""
if not heights:
return 0
res, index = 0, 0
stack = []
while index <= len(heights):
cur = 0 if index == len(heights) else heights[index]
if not stack or cur >= heights[stack[-1]]:
stack.append(index)
index += 1
else:
height = heights[stack.pop()]
# make sure left and right index are correct
right = index - 1
left = 0 if not stack else stack[-1] + 1
res = max(res, (right - left + 1) * height)
return res
[LC] 84. Largest Rectangle in Histogram的更多相关文章
- 84. Largest Rectangle in Histogram
https://www.cnblogs.com/grandyang/p/4322653.html 1.存储一个单调递增的栈 2.如果你不加一个0进去,[1]这种情况就会输出结果0,而不是1 3.单调递 ...
- LeetCode 84. Largest Rectangle in Histogram 单调栈应用
LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...
- 刷题84. Largest Rectangle in Histogram
一.题目说明 题目84. Largest Rectangle in Histogram,给定n个非负整数(每个柱子宽度为1)形成柱状图,求该图的最大面积.题目难度是Hard! 二.我的解答 这是一个 ...
- 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...
- 【LeetCode】84. Largest Rectangle in Histogram
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- 84. Largest Rectangle in Histogram *HARD* -- 柱状图求最大面积 85. Maximal Rectangle *HARD* -- 求01矩阵中的最大矩形
1. Given n non-negative integers representing the histogram's bar height where the width of each bar ...
- 84. Largest Rectangle in Histogram *HARD* -- 求柱状图中的最大矩形面积
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- [LeetCode#84]Largest Rectangle in Histogram
Problem: Given n non-negative integers representing the histogram's bar height where the width of ea ...
- LeetCode OJ 84. Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
随机推荐
- squid完全攻略
squid完全攻略 http://blog.sina.com.cn/s/blog_7572cf8e0100rl99.html squid,nginx,lighttpd反向代理的区别 [root@loc ...
- Ubuntu上运行tensorflow C++的完整例子
个人博客原文:http://www.bearoom.xyz/2019/08/25/ubuntu-tensorflow-cc-example/ 之前记录的运行Tensorflow的C++接口的例子都是零 ...
- go语言实现leetcode-242
package main import ( "fmt" "reflect" ) func isAnagram(s string, t string) bool ...
- 用数组来实现Stack
1:Stack特点 stack:栈,是一种特殊的数据结构,有着先入后出的特点(first in last out).stack中栈底始终不变,只有一端能变化.栈顶在有数据push的时候增大,有数据po ...
- Struts 2的下载和安装
一.为Web应用增加Struts 2支持 下载和安装Struts 2步骤: 登录http://struts.apache.org/download.cgi站点,下载Struts 2的最新版,下载时有以 ...
- Samba 文件共享
介绍:用于Linux系统与Windows系统之间共享文件资源,小文件可以使用lrzsz,大文件可以使用samba 测试环境: [root@localhost home]# cat /etc/redha ...
- 1.where子句的优化
不需要在牺牲可读性的情况下重写sql,因为mysql会自动进行类似的优化. 1.去掉无用的括号 ((a AND b) AND c OR (((a AND b) AND (c AND d)))) -&g ...
- 吴裕雄--天生自然ShellX学习笔记:Shell 变量
定义变量时,变量名不加美元符号($,PHP语言中变量需要),如: your_name="runoob.com" 注意,变量名和等号之间不能有空格,这可能和你熟悉的所有编程语言都不一 ...
- RabbitMQ 整合 SpringCloud实战
RabbitMQ 整合 SpringCloud实战RabbitMQ 整合 SpringCloud实战rabbitmq-common 子项目rabbitmq-springcloud-consumer 子 ...
- 如何正确理解SQL关联子查询
一.基本逻辑 对于外部查询返回的每一行数据,内部查询都要执行一次.在关联子查询中是信息流是双向的.外部查询的每行数据传递一个值给子查询,然后子查询为每一行数据执行一次并返回它的记录.然后,外部查询根据 ...