一天一道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. java new 关键字到底做了什么?

    一.关键字new概述 "new"可以说是Java开发者最常用的关键字,我们使用new创建对象,使用new并通过类加载器来实例化任何我们需要的东西,但你是否深入了解过new在编译的瞬 ...

  2. Python小代码_1_九九乘法表

    Python小代码_1_九九乘法表 max_num = 9 row = 1 while row <= max_num: col = 1 while col <= row: print(st ...

  3. 吴恩达深度学习第1课第4周-任意层人工神经网络(Artificial Neural Network,即ANN)(向量化)手写推导过程(我觉得已经很详细了)

    学习了吴恩达老师深度学习工程师第一门课,受益匪浅,尤其是吴老师所用的符号系统,准确且易区分. 遵循吴老师的符号系统,我对任意层神经网络模型进行了详细的推导,形成笔记. 有人说推导任意层MLP很容易,我 ...

  4. 日常实用css布局技巧汇总

    1.单行完整显示,多行省略显示. .box { width: 100px;  //必要 display: -webkit-box;    //必要 font-size: 14px; line-heig ...

  5. java利用自定义类型对树形数据类型进行排序

    前言 为什么集合在存自定义类型时需要重写equals和hashCode? 1.先说List集合 List集合在存数据时是可以重复的但是 当我们需要判断一个对象是否在集合中存在时这样就有问题了! 因为我 ...

  6. --save-dev 和 --save的区别

    1. 我们在使用npm install xx --save-dev / --save安装模块或插件的时候,会将他们写入到 package.json 文件,那到底有什么区别呢? --save-dev:会 ...

  7. markdowm写博客测试

    markdowm测试文档 #include <bits/stdc++.h> using namespace std; int main() { printf("Hello Wor ...

  8. Bootstrap3 代码-变量

    通过 <var> 标签标记变量. y = mx + b <var>y</var> = <var>m</var><var>x< ...

  9. Activtiy完全解析(三、View的显示过程measure、layout、draw)

    转载请标明出处: http://blog.csdn.net/xmxkf/article/details/52840065 本文出自:[openXu的博客]   在Activity完全解析的第一篇文章A ...

  10. rbac 概念

    1 权限管理 1.1 什么是权限管理 分享牛原创,分享牛系列.基本上涉及到用户参与的系统都要进行权限管理,权限管理属于系统安全的范畴,权限管理实现对用户访问系统的控制,按照安全规则或者安全策略控制用户 ...