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直方图中的最大矩形的更多相关文章

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

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

  2. [LeetCode] 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 直方图里的最大长方形

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

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

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

  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

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

  9. 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...

随机推荐

  1. 使用outflux 导入influxdb 的数据到timescaledb

    influxdb 以及timescaledb 都是不错的时序数据库,timescaledb 团队提供了直接从influxdb 导入 环境准备 docker-compose 文件 version: &q ...

  2. Javascript 来判断数组的假值如 null false "" NaN

    Javascript 来判断数组的假值如 null false "" NaN function bouncer(arr) { arr = arr.filter(function(a ...

  3. node 下less无法编译的问题

    vue+less的项目中,npm run dev不通过,提示以下错误: These dependencies were not found: * !!vue-style-loader!css-load ...

  4. spring boot通过Interceptor和HandlerMethodReturnValueHandler实现统一处理为controller返回对象统计处理时间

    思路:实现思路都是基于Aop实现,方式上可以通过spring aop和spring mvc的aop机制都能实现. 通过Interceptor的可以实现为controller插入开始时间和执行结束时间, ...

  5. JS 60秒后重发送验证码

    //settime($("#getPhoneCode"),60); function settime($obj, time) { if (time == 0) { $obj.att ...

  6. 手把手教你实现 Google 拓展插件(转自实验楼)

    一.课程简介 1.1 实验介绍 本课程的实验环境由实验楼提供,Google 浏览器拓展的运行环境为 Google 浏览器.在本实验中,你将了解如何制作一个属于你自己的 Google 拓展插件. 课程实 ...

  7. Docker三大核心概念及DockerToolBox安装

    一.核心概念 Docker大部分操作都围绕三大概念——镜像.容器和仓库展开. 1.Docker镜像 Docker镜像类似于虚拟机镜像,可以将它理解为一个只读的模板.镜像是创建Docker容器的基础. ...

  8. Entity Framework Code first 可能会导致循环或多个级联路径.

    用code first映射数据库报错 Introducing FOREIGN KEY constraint 'FK_dbo.Roles_dbo.SubSystems_SubSystemID' on t ...

  9. 2、初探 ZooKeeper 技术内幕

    分布式一致性 “分布式” 是大型系统实现高性能.高可用所常用的架构手段,本章节将概述 “分布式一致性”的基本内容,以作为 ZAB 算法阐述的基础. 分布式一致性的基本概念 数据库系统的基础理论中,“事 ...

  10. JAVA使用log4j(另SSM框架中使用log4j)

    1.引入jar包 log4j-1.2.13.jar 2.src下建立配置文件:log4j.properties #不+All,只写后一种LOG log4j.rootLogger =ALL,system ...