一天一道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的更多相关文章

  1. LeetCode 84. Largest Rectangle in Histogram 单调栈应用

    LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...

  2. [LeetCode] 84. Largest Rectangle in Histogram 直方图中最大的矩形

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  3. [LeetCode#84]Largest Rectangle in Histogram

    Problem: Given n non-negative integers representing the histogram's bar height where the width of ea ...

  4. LeetCode 84. Largest Rectangle in Histogram 直方图里的最大长方形

    原题 Given n non-negative integers representing the histogram's bar height where the width of each bar ...

  5. [leetcode]84. Largest Rectangle in Histogram直方图中的最大矩形

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  6. leetCode 84.Largest Rectangle in Histogram (最大矩形直方图) 解题思路和方法

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  7. [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 ...

  8. 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...

  9. 84. Largest Rectangle in Histogram

    https://www.cnblogs.com/grandyang/p/4322653.html 1.存储一个单调递增的栈 2.如果你不加一个0进去,[1]这种情况就会输出结果0,而不是1 3.单调递 ...

  10. 刷题84. Largest Rectangle in Histogram

    一.题目说明 题目84. Largest Rectangle in Histogram,给定n个非负整数(每个柱子宽度为1)形成柱状图,求该图的最大面积.题目难度是Hard! 二.我的解答 这是一个 ...

随机推荐

  1. Lucene——Field.Store(存储域选项)及Field.Index(索引选项)

    Field.Store.YES或者NO(存储域选项) 设置为YES表示或把这个域中的内容完全存储到文件中,方便进行文本的还原 设置为NO表示把这个域的内容不存储到文件中,但是可以被索引,此时内容无法完 ...

  2. 转载c++常忘的知识点

    C++的一些知识点比较零碎,下面清单的形式做一些记录与归纳,以供参考. 1.赋值操作符重载(深复制): (1)由于目标对象可能引用了以前的一些数据,所以应该先delete这些数据: (2)注意到对象可 ...

  3. redis的基本数据类型

    一:redis是一个开源的,使用C语言编写,支持网络,可基于内存亦可持久化的日志型,key-value方式存储的nosql数据库.作为缓存服务器,速度效率都很快,和memcache相似 redis支持 ...

  4. 使用ffmpeg转码时遇到aac报错

    今天尝试用ffmpeg转一个视频的格式,结果报出这个错误: The encoder 'aac' is experimental but experimental codecs are not enab ...

  5. JQuery写的一个常见的banner

    大致的布局如下: <div class="banner" >                <div class="pic">      ...

  6. Docker实例:创建一个点到点连接

    默认情况下,Docker 会将所有容器连接到由 docker0 提供的虚拟子网中. 用户有时候需要两个容器之间可以直连通信,而不用通过主机网桥进行桥接. 解决办法很简单:创建一对 peer 接口,分别 ...

  7. Android开发学习之路--Android Studio cmake编译ffmpeg

      最新的android studio2.2引入了cmake可以很好地实现ndk的编写.这里使用最新的方式,对于以前的android下的ndk编译什么的可以参考之前的文章:Android开发学习之路– ...

  8. Scala:提取器(Extractor)

    http://blog.csdn.net/pipisorry/article/details/52902671 提取器是从传递给它的对象中提取出构造该对象的参数. Scala 标准库包含了一些预定义的 ...

  9. 1.cocos2dx 3.2环境搭建

    1        所需软件 jdk-7u25-windows-i586.exe python-2.7.8.amd64.msi cocos2d-x-3.2.zip apache-ant-1.9.4.zi ...

  10. Universal-Image-Loader 图片异步加载类库的使用

    这个图片异步加载并缓存的类已经被很多开发者所使用,是最常用的几个开源库之一,主流的应用,随便反编译几个火的项目,都可以见到它的身影. 可是有的人并不知道如何去使用这库如何进行配置,网上查到的信息对于刚 ...