[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, 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.
Example:
Input: [2,1,5,6,2,3]
Output: 10
题意:
给定一个直方图,求其能覆盖的最大矩形。
思路:
以下是O(n2) 的时间复杂度解法

当 i= 0 1 2 3 4
area= 2*1 4*1 6*1 5*1 3*1
2*2 4*2 5*2 3*2
2*3 4*3 3*3
2*4 3*4
2*5
以height[i]为临界,每次比较其左侧的所有height谁更小,更新minHeight, 更新当前maxArea
class Solution {
public int largestRectangleArea(int[] heights) {
int maxArea = 0;
for(int i = 0; i < heights.length; i++){
int minHeight = heights[i]; //以height[i]为临界
for(int j = i; j >= 0; j--){ // 每次比较其左侧的所有height谁更小
minHeight = Math.min(minHeight, heights[j]);
maxArea = Math.max(maxArea, minHeight * (i - j + 1));
}
}
return maxArea;
}
}
以下是O(n) 的时间复杂度解法
维护一个单调栈monoStack,数组中每个元素的index都入栈、出栈
代码:
class Solution {
public int largestRectangleArea(int[] heights) {
Stack<Integer> s = new Stack<>();
int area = 0;
for(int i=0; i<= heights.length;){
int value = i< heights.length ? heights[i]: 0;
if(s.isEmpty() || value> heights[s.peek()]){
s.push(i);
i++;
}else{
int temp = s.pop();
area = Math.max(area, heights[temp] * (s.isEmpty() ? i: (i-s.peek()-1)));
}
}
return area;
}
}
[leetcode]84. Largest Rectangle in Histogram直方图中的最大矩形的更多相关文章
- [LeetCode] 84. Largest Rectangle in Histogram 直方图中最大的矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- LeetCode 84. Largest Rectangle in Histogram 直方图里的最大长方形
原题 Given n non-negative integers representing the histogram's bar height where the width of each bar ...
- LeetCode 84. Largest Rectangle in Histogram 单调栈应用
LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...
- 【LeetCode】84. Largest Rectangle in Histogram——直方图最大面积
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- leetCode 84.Largest Rectangle in Histogram (最大矩形直方图) 解题思路和方法
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- [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 ...
- [LeetCode#84]Largest Rectangle in Histogram
Problem: Given n non-negative integers representing the histogram's bar height where the width of ea ...
- 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...
随机推荐
- java应用:向用户注册的邮箱发送邮件
实现功能 忘记密码,注册成功等向用户发送验证码信息或注册信息. 业务流程 忘记密码: 1.验证邮箱是否注册过: 2.向邮箱发送验证码: 3.验证验证码是否正确: 4.重新设置密码: 我这里着重介绍发送 ...
- Python完全新手教程
转发:作者: taowen 来源: 博客园 发布时间: 2010-10-01 00:42 阅读: 1618 次 推荐: 0 原文链接 [收藏] Lesson ...
- Parallel Programming for FPGAs 学习笔记(1)
Parallel Programming for FPGAs 学习笔记(1)
- putty安装和使用
https://blog.csdn.net/l707941510/article/details/80520790
- 【python】参数中的*args和**kwargs
转自https://www.cnblogs.com/xuyuanyuan123/p/6674645.html#undefined 多个实参,放到一个元组里面,以*开头,可以传多个参数:**是形参中按照 ...
- 自定义python扩展类型
目标:自定义一个C\C++矩阵类,有几个用于演示的矩阵运算函数或者操作,将其通过 PyTypeOject newType的方式注册到python中成为一种新的类型,并且要可继承. 预备知识 建议先运行 ...
- [UE4]Text Box
Text Box:文本输入控件. 一.新建一个名为testTextBox的UserWidget,添加一个名为“EditableTextBox_0”的TextBox到默认容器Canvas Panel 二 ...
- Zabbix监控进程(进程消失后钉钉报警)
用于python报警的脚本如下:(钉钉机器人的连接需要修改) #!/usr/bin/python3# -*- coding: utf-8 -*-# Author: aiker@gdedu.ml# My ...
- udev example -- detect usb and write test file
之前学习了下Udev,就随便做了个测试小程序.....设计什么的也没考虑,就实现了一个基本功能,插入U盘,识别,循环检测到有特定文件后,就然后往U盘里面写数据,插拔多次,都能正常工作. 里面的warn ...
- doi
doi是指数字对象唯一标识符,是云计算背景下最佳的“大数据”样本存储和应用技术,用于IKE进行协商SA协议统一分配. doi的优点有唯一性.持久性.兼容性.互操作性.动态更新. 外文名 doi 概 ...