【一天一道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! 二.我的解答 这是一个 ...
随机推荐
- vue开发中遇到的问题集锦(2)
1,在搭建了一个vue的脚手架之后,写了第一个组件,路由也已经配置完毕,且页面的路由显示是:http://localhost:8080/#/userLogin,userLogin里面有内容,但是页面显 ...
- NPOI给单元格加范围边框
HSSFWorkbook workbook2 = new HSSFWorkbook(); //XSSFWorkbook workbook2 = new XSSFWorkbook();// ...
- leetcode刷题笔记342 4的幂
题目描述: 给定一个整数 (32位有符整数型),请写出一个函数来检验它是否是4的幂. 示例:当 num = 16 时 ,返回 true . 当 num = 5时,返回 false. 问题进阶:你能不使 ...
- ng-book札记——HTTP
Angular拥有自己的HTTP库,可以用于调用外部API. 在JavaScript世界里有三种方式可以实现异步请求,Callback,Promise与Observable.Angular倾向于使用O ...
- Spring统一返回Json工具类,带分页信息
前言: 项目做前后端分离时,我们会经常提供Json数据给前端,如果有一个统一的Json格式返回工具类,那么将大大提高开发效率和减低沟通成本. 此Json响应工具类,支持带分页信息,支持泛型,支持Htt ...
- ACM Let the Balloon Rise
Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the ...
- Go 语言常量
常量是一个简单值的标识符,在程序运行时,不会被修改的量. 常量中的数据类型只可以是布尔型.数字型(整数型.浮点型和复数)和字符串型. 常量的定义格式: const identifier [type] ...
- NavigationView使用过程的问题解决
NavigationView是android support design库提供的侧滑面板控件,通常与support v4库里的DrawerLayout侧滑控件搭配使用.以下是使用过程中遇到的问题及解 ...
- python3中替换python2中cmp函数的新函数分析(lt、le、eq、ne、ge、gt)
本文地址:http://blog.csdn.net/sushengmiyan/article/details/11332589 作者:sushengmiyan 在python2中我们经常会使用cmp函 ...
- DB查询分析器7.01新增的周、月SQL执行计划功能
DB查询分析器7.01新增的周.月SQL执行计划功能 马根峰 (广东联合电子服务股份有限公司, 广州 510300) 1 引言 中国本土 ...

