84直方图最大矩形覆盖 · Largest Rectangle in Histogram
[抄题]:
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出来的面积打擂台
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- stack.peek() == 0 时属于非空
- max要初始化为0
- 可以用height[stack.peek()]来运用数组
- i = 0; i <= height.length 都要打印 比如1 1
[二刷]:
- 用三元运算符,curt到头了应该是-1,width在栈为空时就是i本身
- 在括号里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相关推荐
- LeetCode 84. Largest Rectangle in Histogram
LeetCode 84. Largest Rectangle in Histogram Solution1:我的答案 循环里头套了一个动态规划,缺点是当heights个数或最大高度多高时会很耗时间!! ...
- [leetcode]84. Largest Rectangle in Histogram c语言
题目 Given n non-negative integers representing the histogram's bar height where the width of each bar ...
- LeetCode84 Largest Rectangle in Histogram
题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...
- leetcode: Largest Rectangle in Histogram,Maximal Square,Maximal Square问题
Largest Rectangle问题 题目描述 Given n non-negative integers representing the histogram's bar height where ...
- [leetcode]Largest Rectangle in Histogram @ Python
原题地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/ 题意: Given n non-negative integ ...
- LeetCode 84. 柱状图中最大的矩形(Largest Rectangle in Histogram)
题目描述: 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度为 1,给定 ...
- leetcode 84. Largest Rectangle in Histogram | 84. 柱状图中最大的矩形(单调栈)
题目 https://leetcode.com/problems/largest-rectangle-in-histogram/ 题解 一句话总结:遍历数组,对于每个height[i],以其自身的高度 ...
- 84. Largest Rectangle in Histogram
Title 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度为 1,给定 ...
- 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 ...
最新文章
- arcgis api for JavaScript _跨域请求
- python中文什么意思-python中class是什么意思
- Uniform Grid Quadtree kd树 Bounding Volume Hierarchy R树 搜索
- python爬虫网络请求超时_Python网络爬虫编写5-使用代理,处理异常和超时
- 9206-1117-课堂笔记
- 深入浅出Flex组件生命周期Part4 ─ 引擎LayoutManager【转载】
- supervisor+gunicorn部署python web项目
- android判断是否被点击方法,android 中有没有判断imageview是否以被单击的函数方法?...
- Intent中的Flag
- 几何画板椭圆九种画法_椭圆的画法几何画板的动画演示
- el-table合并单元格的方式
- Hypermesh二次开发系列003
- Linux文件目录sha256,在Linux系统中使用SHA256来校验下载的文件的方法
- 26、流程引擎API
- 苹果id退出后数据还在吗_答疑你的手机连接WiFi后,会关闭移动数据吗?
- 苹果笔记本开不了机的解决措施有哪些
- 生产环境SLB-F5会话保持失效问题排查及总结
- python话费充值_Python 登录移动查询话费
- 训练O_net网络,并测试图片
- 【信号与系统】笔记(1)绪论