73.Largest Rectangle in Histogram(最大矩形)
Level:
Hard
题目描述:
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
思路分析:
将每一个height[ i],都作为一个矩形高度的最低点,向两边扩展,找到左右都比它小,停止,计算面积,最后在这些求出的面积中找出最大值。时间复杂度为O(n)。
代码:
public class Solution{
public int largestRectangleArea(int []heights){
if(heights==null||heights.length==0)
return 0;
Stack<Integer>s=new Stack<>(); //存放的是数组的下标
int maxarea=0;
for(int i=0;i<=heights;i++){
int h=(i==heights.length)?0:height[i]; //最后添加一个零是为了让栈中最后一个元素能弹出
if(s.isEmpty()||h>=heights[s.peek()]){ //能保证height[i]左边都比其小
s.push(i);
}else{
int tp=s.pop();
maxarea=Math.max(maxarea,heights[tp]*(s.isEmpty()?i:i-s.peek()-1));
i--;
}
}
return maxarea;
}
}
73.Largest Rectangle in Histogram(最大矩形)的更多相关文章
- Largest Rectangle in Histogram, 求矩形图中最大的长方形面积
问题描述: Given n non-negative integers representing the histogram's bar height where the width of each ...
- LeetCode 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 85--最大矩形(Maximal Rectangle)
84题和85五题 基本是一样的,先说84题 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 思路很简单,通过循环,分别判断第 i 个柱子能够延展的长度le ...
- LeetCode 84. 柱状图中最大的矩形(Largest Rectangle in Histogram)
84. 柱状图中最大的矩形 84. Largest Rectangle in Histogram
- LeetCode 84. 柱状图中最大的矩形(Largest Rectangle in Histogram)
题目描述 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度为 1,给定的 ...
- 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...
- 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 ...
- LeetCode 笔记系列 17 Largest Rectangle in Histogram
题目: Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar he ...
- 47. Largest Rectangle in Histogram && 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的题目,感觉蛮好玩的,于是想呀想呀,怎么求解呢? 还是先把题目贴上来吧 题目写的很直观,就是找直方图的最大矩形面积,不知道是 ...
随机推荐
- Oracle 附加日志(supplemental log)
参考资料: 1.https://blog.csdn.net/li19236/article/details/41621179
- MATLAB 和 armadillo 数据转换
#include<iostream> #include<armadillo> int D=5; int M=4; int main() { arma::fmat x; x.ra ...
- ht-4 hashmap特性
一.hashmap底层原理: hashmap调用默认构造方法会产生一个默认底层是长度为16的Entry数组,首先调用key的hasCode()方法来得到一个整数, int hash = hash(ke ...
- 批处理bat文件显示中文乱码解决方式
1.下载Notepad++并安装 2.选择编码,将文件编码转换为ANSI编码
- 从保障淘宝到全球市场“第一阵营”,阿里云的DDoS防护之路走了多远?
2年前,不少技术圈的朋友,读过论坛里的一篇解读文章:DDoS,阿里为什么要走自己的一条路(https://bbs.aliyun.com/read/271764.html?pos=13),文章讲述了阿里 ...
- 【HDOJ6629】string matching(exkmp)
题意:给定一个长为n的字符串,求其每个位置开始于其自身暴力匹配出相同或不同的结果的总比较次数 n<=1e6 思路:exkmp板子 #include<bits/stdc++.h> us ...
- PHP Timer 页面运行时间监测类
转至:http://blog.csdn.net/fdipzone/article/details/20160567 php Timer 页面运行时间监测类,可按不同key监测不同的运行时间 Timer ...
- sqlserver连接-2
本地连接 方法1. 方法2. 远程连接 如果无法通过IP地址远程连接你的SQL Server 2008服务器,可以参考下面的内容进行设置. 在进行下述设置之前,应该确保你的网络已经安装设置完毕,服务器 ...
- php面试专题---mysql数据库分库分表
php面试专题---mysql数据库分库分表 一.总结 一句话总结: 通过数据切分技术将一个大的MySQLServer切分成多个小的MySQLServer,既攻克了写入性能瓶颈问题,同一时候也再一次提 ...
- vue里面的this指向
this.$http.jsonp(api).then(function(response){ console.log(response); console.log(this); this.list=r ...