【一天一道LeetCode】#84. Largest Rectangle in Histogram
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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 heights = [2,1,5,6,2,3],
return 10.
(二)解题
题目大意:给定一个含有高度值的数组,表示一个直方图,求这组直方图中最大矩阵的面积
解题思路:对于每一个高度值,往左边比它大的话宽度就+1,否则就停止查找,再往右找比它大的话宽度就+1,否则就停止查找,这样就可以计算已当前高度值为高度的最大矩阵面积。
例如:已图中1为例,往左边找比它大的只有1个,往右边找比它大的有4个,这样以1为高度的矩阵宽度为6,这样的话面积为6
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
int max = 0;
for(int i = 0 ; i < heights.size() ; i++)
{
int count = 1;
int j = i -1;
while(j>=0&&heights[j--]>heights[i]) count++;//往左边找
j = i+1;
while(j<heights.size()&&heights[j++]>heights[i]) count++;//往右边找
int area = heights[i]*count;
max = max>area?max:area;//计算面积
}
return max;
}
};
这样做的结果当然是 Time Limit Exceeded!!!
只能继续想办法!
大家可以注意到,每一次往左边找的时候,很多信息都可以利用。
以题目图中的1为例,我们的目的是为了找出1的右边有多少个比它大,
这个时候5的左边有1个比它大,那么就可以跳过6直接看2是否比它大,
然后2的后边3比它大,又可以跳过3。
然后就可以计算出来1的右边有4个比它大!
所以采用两个数组来记录查找过的每一个高度值左/右边比它小的个数。这就是这个算法的重要优化思想!
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
int size = heights.size();
int max= 0;
vector<int> left(size,0);//记录左边比heights[i]大的高度值个数
vector<int> right(size,0);//记录右边比heights[i]大的高度值个数
for(int i = 1 ; i < size ; i++)
{
int j = i-1;
while(j>=0&&heights[j]>=heights[i]) j-=(left[j]+1);//可以跳过left[i]个数,简化了算法的时间复杂度
left[i] = i-j-1;
}
for(int i = size-2; i>=0 ; i--)
{
int j = i+1;
while(j<size&&heights[j]>=heights[i]) j+=(right[j]+1);//同上
right[i] = j-i-1;
}
for(int i = 0 ; i < size ; i++){
int area = (left[i]+right[i]+1)*heights[i];//计算面积
max = max>area?max:area;//取最大
}
return max;
}
};
【一天一道LeetCode】#84. Largest Rectangle in Histogram的更多相关文章
- LeetCode 84. Largest Rectangle in Histogram 单调栈应用
LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...
- [LeetCode] 84. Largest Rectangle in Histogram 直方图中最大的矩形
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 84. Largest Rectangle in Histogram 直方图里的最大长方形
原题 Given n non-negative integers representing the histogram's bar height where the width of each bar ...
- [leetcode]84. Largest Rectangle in Histogram直方图中的最大矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- leetCode 84.Largest Rectangle in Histogram (最大矩形直方图) 解题思路和方法
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- [leetcode]84.Largest Rectangle in Histogram ,O(n)解法剖析
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...
- 84. Largest Rectangle in Histogram
https://www.cnblogs.com/grandyang/p/4322653.html 1.存储一个单调递增的栈 2.如果你不加一个0进去,[1]这种情况就会输出结果0,而不是1 3.单调递 ...
- 刷题84. Largest Rectangle in Histogram
一.题目说明 题目84. Largest Rectangle in Histogram,给定n个非负整数(每个柱子宽度为1)形成柱状图,求该图的最大面积.题目难度是Hard! 二.我的解答 这是一个 ...
随机推荐
- eclipse的maven操作无反应
第一 查eclipse能不能正常用 hi world.java 第二 查maven能不能正常用 cmd: mvn -v 第三 看看maven和eclipse是不是64位之类的 第四 maven和ecl ...
- .net通用CMS快速开发框架——问题1:Dapper通用的多表联合分页查询怎么破?
最近在弄一个东东,类似那种CMS的后台管理系统,方便作为其它项目的初始化框架用的. 现在遇到个问题,如标题所示:Dapper通用的多表联合分页查询怎么破? 难道只能通过拼接sql或者使用存储过程吗?我 ...
- javaweb面试题
1.Tomcat的优化经验 答:去掉对web.xml的监视,把JSP提前编辑成Servlet:有富余物理内存的情况下,加大Tomcat使用的JVM内存. 2.什么是Servlet? 答:可以从两个方面 ...
- js简单备忘录
<section class="myMemory"> <h3 class="f-tit">记事本</h3> <div ...
- Git 常用命令速查表
- oo第二阶段总结
第五次作业--多线程电梯 一.设计策略 本次作业是我们第一次接触多线程,给程序添加多线程功能后最大的挑战是实现共享数据的安全.避免冲突,由于这次作业是第一次尝试多线程方法,因此采用了将所有方法都加上s ...
- Xcode编译错误__NSCFConstantString
__NSCFConstantString:主要错误就是数据类型造成的,然后就是检查哪个地方造成的数据类型调用错误 错误一:'-[__NSCFConstantString _imageThatSuppr ...
- MySQL注释中的sql也可能执行
MySql支持三种注释形式:# 和–属于单行注释,注释范围为该行的结尾:/* */注释属于多行注释,此外该种注释还可以实现行内注释.具体的使用情况如下图中所示(四种使用情形): 除此之外,/* */这 ...
- Android自定义View(LineBreakLayout-自动换行的标签容器)
最近一段时间比较忙,都没有时间更新博客,今天公司的事情忙完得空,继续为我的自定义控件系列博客添砖加瓦.本篇博客讲解的是标签自动换行的布局容器,正好前一阵子有个项目中需要,想了想没什么难度就自己弄了 ...
- 【SSH系列】深入浅出spring IOC中三种依赖注入方式
spring的核心思想是IOC和AOP,IOC-控制反转,是一个重要的面向对象编程的法则来消减计算机程序的耦合问题,控制反转一般分为两种类型,依赖注入和依赖查找,依赖什么?为什么需要依赖?注入什么?控 ...