Level:

  Hard

题目描述:

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

思路分析:

  将每一个height[ i],都作为一个矩形高度的最低点,向两边扩展,找到左右都比它小,停止,计算面积,最后在这些求出的面积中找出最大值。时间复杂度为O(n)。

代码:

public class Solution{
public int largestRectangleArea(int []heights){
if(heights==null||heights.length==0)
return 0;
Stack<Integer>s=new Stack<>(); //存放的是数组的下标
int maxarea=0;
for(int i=0;i<=heights;i++){
int h=(i==heights.length)?0:height[i]; //最后添加一个零是为了让栈中最后一个元素能弹出
if(s.isEmpty()||h>=heights[s.peek()]){ //能保证height[i]左边都比其小
s.push(i); }else{
int tp=s.pop();
maxarea=Math.max(maxarea,heights[tp]*(s.isEmpty()?i:i-s.peek()-1));
i--;
}
}
return maxarea;
}
}

73.Largest Rectangle in Histogram(最大矩形)的更多相关文章

  1. Largest Rectangle in Histogram, 求矩形图中最大的长方形面积

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

  2. LeetCode 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 85--最大矩形(Maximal Rectangle)

    84题和85五题 基本是一样的,先说84题 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 思路很简单,通过循环,分别判断第 i 个柱子能够延展的长度le ...

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

    84. 柱状图中最大的矩形 84. Largest Rectangle in Histogram

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

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

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

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

  6. 84. Largest Rectangle in Histogram *HARD* -- 柱状图求最大面积 85. Maximal Rectangle *HARD* -- 求01矩阵中的最大矩形

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

  7. LeetCode 笔记系列 17 Largest Rectangle in Histogram

    题目: Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar he ...

  8. 47. Largest Rectangle in Histogram && Maximal Rectangle

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

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

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

随机推荐

  1. Object中有哪些公用方法?

    clone()方法 实现对象的浅复制,只有实现了Cloneable接口才能调用该方法. toString()方法 返回该对象的字符串表示. equals()方法: 在Object中与“==”的定义是一 ...

  2. 解压版(.zip)的MySQL数据库下载安装

    1.首先到mysql官网下载(https://dev.mysql.com/downloads/mysql/5.6.html#downloads): 2.解压下载好的压缩包文件,并将解压后的文件夹放到合 ...

  3. Mybatis学习笔记大纲

    Mybatis学习笔记大纲: 一.MyBatis简介 二.MyBatis-HelloWorld 三.MyBatis-全局配置文件 四.MyBatis-映射文件 五.MyBatis-动态SQL 六.My ...

  4. LDD3 第15章 内存映射和DMA

    本章内容分为三个部分: 第一部分讲述了mmap系统调用的实现过程.将设备内存直接映射到用户进程的地址空间,尽管不是所有设备都需要,但是能显著的提高设备性能. 如何跨越边界直接访问用户空间的内存页,一些 ...

  5. 51nod 1514 美妙的序列 分治NTT + 容斥

    Code: #include<bits/stdc++.h> #define ll long long #define mod 998244353 #define maxn 400000 # ...

  6. 【Java】Java调用第三方接口

    Get请求与Http请求 https://www.w3school.com.cn/tags/html_ref_httpmethods.asp HttpClient HTTP 协议可能是现在 Inter ...

  7. 【面经分享】前端小白半年准备,成功进入bat

    先介绍下背景 非211,985本科毕业.一年半PHP经验,一年半前端经验,前端一直在做React开发. 半年之前,我是一个前端小小小白.多么小白呢? css调样式全靠试. 盒模型,好像知道是啥?好像又 ...

  8. 51nod 1205 流水线调度

    1205 流水线调度 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 N个作业{1,2,…,n}要在由2台机器M1和M2组成的流水线上完成加工.每个 ...

  9. MySQL5.6同步指定数据库

    需求:生产环境同步sakila (root@localhost) [(none)]> show databases;+--------------------+| Database |+---- ...

  10. 【CDN+】 Spark入门---Handoop 中的MapReduce计算模型

    前言 项目中运用了Spark进行Kafka集群下面的数据消费,本文作为一个Spark入门文章/笔记,介绍下Spark基本概念以及MapReduce模型 Spark的基本概念: 官网: http://s ...