首先上题目

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.

For example,
Given height = [2,1,5,6,2,3],
return 10.

题目的大意即给出一个柱状图的歌数据,求柱状图内所包含的最大矩形。

这道题目想了较长时间,助教说用栈的思想一开始也不是很明白(看来真的把数据结构灵活用到实际问题还是很难啊)。后来想用栈的话,目标肯定是O(n),基础操作不外乎前后项比较,根据比较结果进行不同操作,然后有了以下思路。

思路一

初始图表数据项是无序的,用栈进行操作剪掉非最优解后,我们要实现一个逐项递增的效果,如:1 1 1 2 2 4 4……这样的图表无疑是易于计算的。之所以用栈是因为,可以比较得出一个数据n的前后连续数据中,有几个不小于n,压栈完成了前计数,退栈完成了后计数。

LeetCode之Largest Rectangle in Histogram浅析的更多相关文章

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

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

  2. leetcode之Largest Rectangle in Histogram

    问题来源:Largest Rectangle in Histogram 问题描述:给定一个长度为n的直方图,我们可以在直方图高低不同的长方形之间画一个更大的长方形,求该长方形的最大面积.例如,给定下述 ...

  3. Java for LeetCode 084 Largest Rectangle in Histogram【HARD】

    For example, Given height = [2,1,5,6,2,3], return 10. 解题思路: 参考Problem H: Largest Rectangle in a Hist ...

  4. 关于LeetCode的Largest Rectangle in Histogram的低级解法

    在某篇博客见到的Largest Rectangle in Histogram的题目,感觉蛮好玩的,于是想呀想呀,怎么求解呢? 还是先把题目贴上来吧 题目写的很直观,就是找直方图的最大矩形面积,不知道是 ...

  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 OJ] 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

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

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

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

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

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

随机推荐

  1. 【解决】putty使用从AWS下载的private key登录失败

    在AWS启动一个实例时如果创建并下载了一个KeyPair的私钥(*.pem),则可以此私钥作为Credentials通过putty远程登录到这个实例系统.但在实际操作中,用putty登录时会提示如下错 ...

  2. Genymotion出现virtualbox cannot start the virtual device错误

    选择你要启动的device右侧的设置 打开如下界面 将Processor设置为1 (默认为4)

  3. 《从零开始做一个MEAN全栈项目》(4)

    欢迎关注本人的微信公众号"前端小填填",专注前端技术的基础和项目开发的学习. 在上一篇中,我们讲了如何去构建第一个Express项目,总结起来就是使用两个核心工具,express和 ...

  4. javascript this关键字指向详解

    在之前写代码的经历中,常常试过写着写着this就莫名其妙的不知道指向到哪里去了.今天看了曾探的javascript设计模式,里面特别谈到了this在不同情况下指代的对象,非常有意思. this指代的情 ...

  5. Sublime插件安装

    来在Sublime text3上安装Package Control 使用Ctrl+`(ESC下边的那个~)快捷键或者通过View->Show Console菜单打开命令行,粘贴如下代码: imp ...

  6. leetcode 202

    202. Happy Number Write an algorithm to determine if a number is "happy". A happy number i ...

  7. leetcode 191

    191. Number of 1 Bits Write a function that takes an unsigned integer and returns the number of ’1' ...

  8. xhprof学习笔记

    一.简介 XHProf 是一个轻量级的分层性能测量分析器. 在数据收集阶段,它跟踪调用次数与测量数据,展示程序动态调用的弧线图. 它在报告.后期处理阶段计算了独占的性能度量,例如运行经过的时间.CPU ...

  9. C# 生成表格代碼

    public ActionResult btnExport(ReportViewModel model)         {             //接收需要导出的数据             L ...

  10. python字符串及其方法详解

    首先来一段字符串的基本操作 str1="my little pony" str2="friendship is magic" str3=str1+", ...