一天一道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. gulp填坑记(一)

    gulp是基于Node.js的自动任务运行器.可以自动完成html.image.css和js等文件的检测.检查.合并.压缩.格式化等,并监听文件在改动后重复指定的这些步骤. 一.首先,我全局安装了gu ...

  2. 使用foreach需要判空。

    今天写代码的时候,需要遍历一个作为参数传递进来的容器, 当时顺手就加上了判空条件: if(null==list)return; 后来就像,不知道遍历(foreach)有没有帮我做这个工作: 下面看实验 ...

  3. 地址下拉框,需要js级联js

    function area() { _url = "/ashx/DropDownControl.ashx"; _swType = "GetArea"; _z = ...

  4. ACM 人见人爱A^B

    求A^B的最后三位数表示的整数. 说明:A^B的含义是"A的B次方"  Input输入数据包含多个测试实例,每个实例占一行,由两个正整数A和B组成(1<=A,B<=10 ...

  5. Python3 条件控制

    if 语句 Python中if语句的一般形式如下所示: if condition_1: statement_block_1 elif condition_2: statement_block_2 el ...

  6. OC基础之推荐一个旋转木马(跑马灯)效果的图片展示Demo

    这个旋转木马(跑马灯)效果的图片展示Demo,包括设定旋转方向,图片倒影,背景设置,旋转速度,开始结束,点击显示选中的图片,彩色的块展示等等功能 效果图:(源码下载:https://github.co ...

  7. Rails里rake db:migrate出现undefined method last_comment问题的解决

    这个问题和特定的rake版本有关,因为Rails要使用rake的last_comment方法在较新版本的rake中已被废弃,所以很多人卸载了新版本的rake去安装旧版本的rake. 这样也能解决问题, ...

  8. RxJava(十一)defer操作符实现代码支持链式调用

    欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/52597643 本文出自:[余志强的博客] 一.前言 现在越来越多An ...

  9. Excel init

    Sub Test() Dim r As Range Dim a As Integer a = For Each r In Range("b1:b6") If r.Font.Bold ...

  10. hive 压缩全解读(hive表存储格式以及外部表直接加载压缩格式数据);HADOOP存储数据压缩方案对比(LZO,gz,ORC)

    数据做压缩和解压缩会增加CPU的开销,但可以最大程度的减少文件所需的磁盘空间和网络I/O的开销,所以最好对那些I/O密集型的作业使用数据压缩,cpu密集型,使用压缩反而会降低性能. 而hive中间结果 ...