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].

思路:用动态规划。

法I:首先想到用两个数组,分别存储向左最大延伸位置以及向右最大延伸位置,这种方法要遍历两边,且空间复杂度O(n)+O(n)

法II:用stack的栈顶元素指示所要计算的height,左界是栈顶第二个元素+1位置(因为只有栈顶永远是栈中高度最高的元素),右界是当前元素-1位置(当前元素比栈顶元素低的情况下)。当当前元素高度>= 栈顶元素高度,入栈,i++;当当前元素高度<栈顶元素高度,出栈,计算当前面积,i不增加。

class Solution {
public int largestRectangleArea(int[] heights) {
Stack<Integer> st = new Stack<Integer>();
if(heights.length == 0) return 0; int leftIndex;
int topIndex;
int area;
int maxArea = 0;
int i = 0;
while(i < heights.length){
if(st.empty() || heights[i] >= heights[st.peek()]){
st.push(i);
i++;
}
else{
topIndex = st.peek();
st.pop();
leftIndex = st.empty()?0:st.peek()+1; //如果是空,说明从头开始的所有高度都比heights[topIndex]高
area = ((i-1)-leftIndex + 1) * heights[topIndex];
if(area > maxArea) maxArea = area;
}
}
while(!st.empty()){ //没有比栈顶元素小的元素让它出栈
topIndex = st.peek();
st.pop();
leftIndex = st.empty()?0:st.peek()+1;
area = ((i-1)-leftIndex + 1) * heights[topIndex];
if(area > maxArea) maxArea = area;
}
return maxArea;
}
}

84. Largest Rectangle in Histogram (JAVA)的更多相关文章

  1. 84. Largest Rectangle in Histogram

    https://www.cnblogs.com/grandyang/p/4322653.html 1.存储一个单调递增的栈 2.如果你不加一个0进去,[1]这种情况就会输出结果0,而不是1 3.单调递 ...

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

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

  3. 刷题84. Largest Rectangle in Histogram

    一.题目说明 题目84. Largest Rectangle in Histogram,给定n个非负整数(每个柱子宽度为1)形成柱状图,求该图的最大面积.题目难度是Hard! 二.我的解答 这是一个 ...

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

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

  5. 【LeetCode】84. Largest Rectangle in Histogram

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

  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 OJ 84. Largest Rectangle in Histogram

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

  8. 84. Largest Rectangle in Histogram *HARD* -- 求柱状图中的最大矩形面积

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

  9. [LeetCode#84]Largest Rectangle in Histogram

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

随机推荐

  1. 第五周学习总结&实验报告三

    第五周课程总结 1.this和super的区别: this:访问本类中的属性,如果本类没有此属性则从父类中继续查找:访问本类中的方法,如果本类中没有此方法则从父类中继续查找:调用本类构造,必须放在构造 ...

  2. spark 笔记 6: RDD

    了解RDD之前,必读UCB的论文,个人认为这是最好的资料,没有之一. http://www.cs.berkeley.edu/~matei/papers/2012/nsdi_spark.pdf A Re ...

  3. nginx-location正则表达式匹配规则及动静分离

    nginx-location正则表达式匹配规则及动静分离  发表于 2018年03月5日 |  分类于 nginx|  0 nginx,location常用正则表达式,及nginx动静分离 nginx ...

  4. Dubbo Admin 控制台

    下载项目源码 https://github.com/apache/incubator-dubbo-admin 解压后配置 zookeeper 地址,路径为 dubbo-admin-server/src ...

  5. Java中的23种设计模式与7大原则

    一.创建型模式 1.抽象工厂模式(Abstract factory pattern): 提供一个接口, 用于创建相关或依赖对象的家族, 而不需要指定具体类.2.生成器模式(Builder patter ...

  6. Android 开源控件与常用开发框架开发工具类

    Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 bui ...

  7. 用xmmp+openfire+smack搭建简易IM实现

    功能实现:注册,登录,单聊表情,文本,图片,语音的发送接收,添加好友,删除好友,查找好友,修改密码,消息提醒设置,获取离线消息等功能 1.前期准备 1.下载opnefire软件:https://www ...

  8. Upload 上传

    通过点击或者拖拽上传文件 点击上传 通过 slot 你可以传入自定义的上传按钮类型和文字提示.可通过设置limit和on-exceed来限制上传文件的个数和定义超出限制时的行为.可通过设置before ...

  9. svn导出项目到myeclipse,运行报ClassNotFoundException

    一开始以为是 这样的svn导出项目到myeclipse,运行报ClassNotFoundException 后来不行 又看了一下  还不行 以为是这样的MyEclipse2014报错java.lang ...

  10. java:(九大内置对象,计算服务器访问次数,filter过滤器,MVC框架,MVC和三层架构的关系)

    1.九大内置对象: <%@ page language="java" import="java.util.*" pageEncoding="UT ...