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没做出来,看答案刷的: 艾宾浩斯遗忘曲线 ...
随机推荐
- Master-Worker设计模式介绍
Master-Worker模式是常用的并行设计模式.核心思想是,系统由两个角色组成,Master和Worker,Master负责接收和分配任务,Worker负责处理子任务.任务处理过程中,Master ...
- Java学习笔记42(数据库连接池 druid连接池)
druid连接池: 是阿里的连接池,druid的稳定性及效率都很高,目前用的比较广,所以建议开发过程中尽量用druid连接池(支持国产最重要) druid连接池也需要配置文件,配置文件必须是prope ...
- 数据文件resize回收空间
场景说明: 客户 ASM磁盘组,data磁盘组空闲空间90G,空间不足,因此强烈建议回收空间 空间回收方案: 1.数据文件resize,回收部分可用性空间(好处就是能够将ASM磁盘组free大小增加) ...
- spring jpa + mybatis快速开始:
springmvc开始搭建 源码地址 https://gitee.com/flydb/spingjpamy pom: <packaging>war</packaging> &l ...
- centos安装tomcat步骤
1.官网或wget下载apache-tomcat-8.5.9.tar.gz: cd /home/tar wget xxxxxxxxxx cp /home/tar/apache-tomcat-8.5.9 ...
- Windows下struct和union字节对齐设置以及大小的确定(一 简介和结构体大小的确定)
在windows下设置字节对齐大小的方式,目前我了解有三种: 1. 在编译程序时候的编译选项 /Zp[n],如 cl /Zp4 表示对齐大小是4字节: 2. 预处理命令 #pragma pack ...
- 集合总结五(Hashtable的实现原理)
一.概述 上一篇介绍了Java8的HashMap,接下来准备介绍一下Hashtable. Hashtable可以说已经具有一定的历史了,现在也很少使用到Hashtable了,更多的是使用HashMap ...
- 原生js创建模态框(摘自:东窗凝残月 链接:https://www.cnblogs.com/dcncy/p/9076937.html)
<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>Te ...
- sequelize的get/post方法例子
定义两个model,一个给get的,一个给post的 var Sequelize = require('sequelize'); const DeviceNos = sequelize.define( ...
- iterator简单描述
Item 26. Prefer iterator to const iterator, reverse_iterator, and const_reverse_iterator. 上面一段话,是< ...