leetcode84
public class Solution
{
public int LargestRectangleArea(int[] hist)
{
// The main function to find the
// maximum rectangular area under
// given histogram with n bars
int n = hist.Length;
// Create an empty stack. The stack
// holds indexes of hist[] array
// The bars stored in stack are always
// in increasing order of their heights.
Stack<int> s = new Stack<int>(); int max_area = ; // Initialize max area
int tp; // To store top of stack
int area_with_top; // To store area with top
// bar as the smallest bar // Run through all bars of
// given histogram
int i = ;
while (i < n)
{
// If this bar is higher than the
// bar on top stack, push it to stack
if (s.Count == || hist[s.Peek()] <= hist[i])
{
s.Push(i++);
} // If this bar is lower than top of stack,
// then calculate area of rectangle with
// stack top as the smallest (or minimum
// height) bar. 'i' is 'right index' for
// the top and element before top in stack
// is 'left index'
else
{
tp = s.Peek(); // store the top index
s.Pop(); // pop the top // Calculate the area with hist[tp]
// stack as smallest bar
area_with_top = hist[tp] *
(s.Count == ? i : i - s.Peek() - ); // update max area, if needed
if (max_area < area_with_top)
{
max_area = area_with_top;
}
}
} // Now pop the remaining bars from
// stack and calculate area with every
// popped bar as the smallest bar
while (s.Count > )
{
tp = s.Peek();
s.Pop();
area_with_top = hist[tp] *
(s.Count == ? i : i - s.Peek() - ); if (max_area < area_with_top)
{
max_area = area_with_top;
}
} return max_area;
}
}
参考:https://www.geeksforgeeks.org/largest-rectangle-under-histogram/
补充一个python的实现:
class Solution:
def largestRectangleArea(self, heights: 'List[int]') -> int:
heights.append(0)#默认最后补充一个0,方便统一处理
n = len(heights)
s = []
max_area = 0#最大面积
tp = 0#栈顶索引
area_with_top = 0#临时面积
i = 0
while i < n:
if len(s) == 0 or heights[s[-1]] <= heights[i]:
s.append(i)#栈内记录的是高度递增的索引
i += 1
else:#遇到了高度比当前栈顶元素低的元素时,
tp = s.pop(-1)#清算栈内的元素的高度
if len(s) == 0:
area_with_top = heights[tp] * i#栈内没有元素,则宽度是i
else:#高度是栈顶元素,宽度是i - 1 - 前一个栈顶元素的索引
area_with_top = heights[tp] * (i - s[-1] - 1)
max_area = max(max_area,area_with_top)#更新最大值
# while len(s) > 0:#处理栈内剩余元素,处理流程和遇到一个
# tp = s.pop(-1)
# if len(s) == 0:
# area_with_top = heights[tp] * i
# else:
# area_with_top = heights[tp] * (i - s[-1] - 1)
# max_area = max(max_area,area_with_top)
return max_area
采用了一个小技巧,在heights最后补一个0,则21行到27行的代码就可以省略了。
leetcode84的更多相关文章
- [Swift]LeetCode84. 柱状图中最大的矩形 | Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- 【leetcode-84】 柱状图中最大的矩形
(1pass,比较简单的hard) 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每 ...
- leetcode84 Largest Rectangle in Histogram
思路: 使用单调栈计算每个位置左边第一个比它矮的位置和右边第一个比它矮的位置即可. 实现: #include <bits/stdc++.h> using namespace std; cl ...
- Leetcode84. 柱状图中最大的矩形(单调栈)
84. 柱状图中最大的矩形 前置 单调栈 做法 连续区间组成的矩形,是看最短的那一块,求出每一块左边第一个小于其高度的位置,右边也同理,此块作为最短限制.需要两次单调栈 单调栈维护递增区间,每次不满足 ...
- leetcode84 柱状图
O(n^2) time 应用heights[r]<=heights[r+1]剪枝: class Solution { public: int largestRectangleArea(vecto ...
- LeetCode---84. 柱状图中最大的矩形(hard)
题目:84. 柱状图中最大的矩形 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 示例: 输入: [2,1,5 ...
- LeetCode (85): Maximal Rectangle [含84题分析]
链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...
- LeetCode85 Maximal Rectangle java题解
public static int maximalRectangle(char[][] matrix) { int rowNum=matrix.length; if(rowNum==0) return ...
- leetcode 日常清单
a:excellent几乎一次ac或只有点小bug很快解决:半年后再重刷: b:经过艰难的debug和磕磕绊绊或者看了小提示才刷出来: c:经过艰难的debug没做出来,看答案刷的: 艾宾浩斯遗忘曲线 ...
随机推荐
- RHEL 6 和 RHEL 7 的一些有关运行级别,服务管理,服务启动等方面的区别介绍
systemd是7中的新命令组,集成了service和chkconfig的功能.system命令可参考:https://www.cnblogs.com/ray-bk/p/10415173.html 运 ...
- 理解微信小程序的生命周期和运行原理
写微信小程序,他的生命周期不能不知道,不知道小程序就会出现各种bug而无法解决.小助君公众号带你学习小程序的生命周期和运行原理. 小程序由两大线程组成:负责界面的线程(view thread)和服务线 ...
- 1.6 selenium3+firefox环境搭建
1.6 selenium3+firefox环境搭建 有不少小伙伴在安装selenium环境后启动firefox报错,因为现在selenium升级到3.0了,跟2.0的版本还有有一点区别的.(备注:这里 ...
- Go Example--状态协程
package main import ( "fmt" "math/rand" "sync/atomic" "time" ...
- 浅谈一下mshta在CVE-2017-11882里的命令构造
Evi1cg同学前不久放出CVE-2017-11882的一个 python利用脚本,地址在https://github.com/Ridter/CVE-2017-11882/,不过其中一个版本里边有一个 ...
- PythonStudy——逻辑运算符 Logical Operators
在Python中,None.任何数值类型中的0.空字符串“”.空元组().空列表[].空字典{}都被当作False,还有自定义类型,如果实现了 __ nonzero __ () 或 __ len __ ...
- SpringData JPA框架使用时出现JSON循环依赖解决方案
困扰许久的问题终于解决了,之前项目太赶,没有深入学习解决,不甘心,今天再次搭起架子试试,哈哈,终于解决! @ManyToOne(cascade={CascadeType.MERGE,CascadeTy ...
- hbase hbck命令
hbase hbck 只做检查 hbase hbck -fixMeta 根据region目录中的.regioninfo,生成meta表` hbase hbck -fixAssignments 把met ...
- Spring boot 配置 mybatis xml和动态SQL 分页配置
更新时间 2018年4月30日23:27:07 1.pom.xml <?xml version="1.0" encoding="UTF-8"?> & ...
- dependency walker检查dll依赖关系目录设置的问题
废话少说,直接上图 图中来看,似乎IESHIMS.DLL文件不存在报错,实际是因为没有加载IESHIMS.DLL所在的路径. 在我的电脑里面搜索有两个同名的dll,一个是32位的,一个是64位的. C ...