[抄题]:

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.

[思维问题]:

[一句话思路]:

小高度使得全部大高度都终结了,对POP出来的面积打擂台

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. stack.peek() == 0 时属于非空
  2. max要初始化为0
  3. 可以用height[stack.peek()]来运用数组
  4. i = 0; i <= height.length 都要打印 比如1 1

[二刷]:

  1. 用三元运算符,curt到头了应该是-1,width在栈为空时就是i本身
  2. 在括号里pop了也算是完成了pop,不用再pop了

[总结]:

[复杂度]:Time complexity: O(n) push了n个但是只会pop出来一个 Space complexity: O(n)

[英文数据结构,为什么不用别的数据结构]:

[其他解法]:

[Follow Up]:

85 1拼出的最大矩形

[题目变变变]:

public class Solution {public int largestRectangleArea(int[] height) {if (height == null || height.length == 0) {return 0;}Stack<Integer> stack = new Stack<Integer>();int max = 0;for (int i = 0; i <= height.length; i++) {int curt = (i == height.length) ? -1 : height[i];while (!stack.isEmpty() && curt <= height[stack.peek()]) {int h = height[stack.pop()];int w = stack.isEmpty() ? i : i - stack.peek() - 1;max = Math.max(max, h * w);}stack.push(i);}return max;}
}

View Code

转载于:https://www.cnblogs.com/immiao0319/p/8250154.html

84直方图最大矩形覆盖 · Largest Rectangle in Histogram相关推荐

  1. LeetCode 84. Largest Rectangle in Histogram

    LeetCode 84. Largest Rectangle in Histogram Solution1:我的答案 循环里头套了一个动态规划,缺点是当heights个数或最大高度多高时会很耗时间!! ...

  2. [leetcode]84. Largest Rectangle in Histogram c语言

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

  3. LeetCode84 Largest Rectangle in Histogram

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

  4. leetcode: Largest Rectangle in Histogram,Maximal Square,Maximal Square问题

    Largest Rectangle问题 题目描述 Given n non-negative integers representing the histogram's bar height where ...

  5. [leetcode]Largest Rectangle in Histogram @ Python

    原题地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/ 题意: Given n non-negative integ ...

  6. LeetCode 84. 柱状图中最大的矩形(Largest Rectangle in Histogram)

    题目描述: 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度为 1,给定 ...

  7. leetcode 84. Largest Rectangle in Histogram | 84. 柱状图中最大的矩形(单调栈)

    题目 https://leetcode.com/problems/largest-rectangle-in-histogram/ 题解 一句话总结:遍历数组,对于每个height[i],以其自身的高度 ...

  8. 84. Largest Rectangle in Histogram

    Title 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度为 1,给定 ...

  9. LeetCode hard 84. Largest Rectangle in Histogram--python,java 15行,c++ 15行 解法

    题目地址: Given n non-negative integers representing the histogram's bar height where the width of each ...

最新文章

  1. arcgis api for JavaScript _跨域请求
  2. python中文什么意思-python中class是什么意思
  3. Uniform Grid Quadtree kd树 Bounding Volume Hierarchy R树 搜索
  4. python爬虫网络请求超时_Python网络爬虫编写5-使用代理,处理异常和超时
  5. 9206-1117-课堂笔记
  6. 深入浅出Flex组件生命周期Part4 ─ 引擎LayoutManager【转载】
  7. supervisor+gunicorn部署python web项目
  8. android判断是否被点击方法,android 中有没有判断imageview是否以被单击的函数方法?...
  9. Intent中的Flag
  10. 几何画板椭圆九种画法_椭圆的画法几何画板的动画演示
  11. el-table合并单元格的方式
  12. Hypermesh二次开发系列003
  13. Linux文件目录sha256,在Linux系统中使用SHA256来校验下载的文件的方法
  14. 26、流程引擎API
  15. 苹果id退出后数据还在吗_答疑你的手机连接WiFi后,会关闭移动数据吗?
  16. 苹果笔记本开不了机的解决措施有哪些
  17. 生产环境SLB-F5会话保持失效问题排查及总结
  18. python话费充值_Python 登录移动查询话费
  19. 训练O_net网络,并测试图片
  20. 【信号与系统】笔记(1)绪论

热门文章

  1. 如何用15天摸清一个新的行业?
  2. python%20是什么东西
  3. 晚上没有路灯,我骑的很慢
  4. 为什么电视那么大,才2、3千元;手机那么小要5、6千元?
  5. 为什么越来越多的人尝试做自媒体
  6. 挣钱难,做老板不易,特别是种植老板
  7. 同一机器部署多个JVM
  8. 如何用手机NFC代替小区门禁?
  9. 为什么很多人不跑滴滴了?
  10. CSS--CSS清除浮动的4种方式