【一天一道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! 二.我的解答 这是一个 ...
随机推荐
- redis分布式锁-SETNX实现
Redis有一系列的命令,特点是以NX结尾,NX是Not eXists的缩写,如SETNX命令就应该理解为:SET if Not eXists.这系列的命令非常有用,这里讲使用SETNX来实现分布式锁 ...
- vue.js-路由
1:编写router.js import Router from "vue-router" import Vue from "vue" import rou ...
- 实验与作业(Python)-05 程序的控制结构
推荐完成顺序: 1->2->3->4.1->4.4->5->4.5->4.7->6 截止日期 下次实验课之前 实验目标 if-elif-else 循环: ...
- 利用git pull的勾子实现敏捷部署
监听端 例如nginx或Python,php,rails等后端 git --git-dir=~/op/.git --work-tree=~/op pull git hooks端 位于.git/hook ...
- 为什么内部类访问的外部变量需要使用final修饰
因为生命周期的原因.方法中的局部变量,方法结束后这个变量就要释放掉,final保证这个变量始终指向一个对象.首先,内部类和外部类其实是处于同一个级别,内部类不会因为定义在方法中就会随着方法的执行完毕而 ...
- Mac入门——快捷键
快捷键: 截图 Shift+Command+3 截取全屏幕至桌面 Shift+Command+4 截取部分屏幕至桌面 Shift+Command+4+空格 截取窗口或原件至桌面 Shift+ ...
- Objective-C基础之简析深浅copy
一.从面向对象到Objective-C概览copy 1.面向对象: In object-oriented programming, object copying is creating a copy ...
- Android Studio 2.2 新功能详解
Tamic /文 -译 http://blog.csdn.net/sk719887916/article/details/52672688 Android的Studio 2.2 已经可以在官网下载了. ...
- Android Multimedia框架总结(十二)CodeC部分之OMXCodec与OMX事件回调流程
转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52629449 前言:上篇文中分析 ...
- android galley实现画廊效果
青春流水指间. 每段路,都有即将要来的旅程 每颗心,都有值得期待的成分 Android之ImageSwitcher,Gallery用法 今天在做一个软件界面时用到了ImageSwitcher和Gall ...