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. spark MLlib 概念 3: 卡方分布(chi-squared distribution)

    数学定义[编辑] 若k个随机变量.--.是相互独立,符合标准正态分布的随机变量(数学期望为0.方差为1),则随机变量Z的平方和 被称为服从自由度为 k 的卡方分布,记作 Definition[edit ...

  2. fastjson解析list ,object中含有list, object中含有map

    1.首先定义测试vo package com.haiyisoft.cAssistantWeb.ui; import java.sql.Timestamp; public class vo {priva ...

  3. 关于Oracle报表

    1.存储过程中的WHEN OTHERS THEN是什么意思. 异常分很多种类,如NO_FOUND.OTHERS处本应该写异常名称,如果不想把异常分得那么细,可以笼统一点用OTHERS来捕获,即所有异常 ...

  4. tensorflow service部署

    tensorflow+tensorflow-serving+docker+grpc模型上线部署(不需bazel编译,有代码)[https://blog.csdn.net/u013714645/arti ...

  5. Ajax中Put和Delete请求传递参数无效的解决方法(Restful风格)

    本文装载自:http://blog.csdn.net/u012737182/article/details/52831008    感谢原文作者分享 开发环境:Tomcat9.0 在使用Ajax实现R ...

  6. 阶段3 2.Spring_04.Spring的常用注解_4 由Component衍生的注解

    为什么要使用者三个注解 Controller:表现层 Service:业务层 Repository:持久层 在这里就是用Controller 运行也没问题 用Service Repository同样也 ...

  7. 【ABAP系列】SAP ABAP OOALV 动态设置单元格可否编辑

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP OOALV 动 ...

  8. 【ABAP系列】SAP BOM反查

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP BOM反查   前言部分 ...

  9. java并发编程 线程基础

    java并发编程 线程基础 1. java中的多线程 java是天生多线程的,可以通过启动一个main方法,查看main方法启动的同时有多少线程同时启动 public class OnlyMain { ...

  10. python学习之模块-模块(三)

    5.6 time 模块 已经知道的常用的time方法:time.time()获取当前时间的时间戳:time.sleep(num)线程推迟指定的时间(秒)后再继续往下运行. 时间的表示方式 大致可以分为 ...