84. Largest Rectangle in Histogram (JAVA)
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)的更多相关文章
- 84. Largest Rectangle in Histogram
https://www.cnblogs.com/grandyang/p/4322653.html 1.存储一个单调递增的栈 2.如果你不加一个0进去,[1]这种情况就会输出结果0,而不是1 3.单调递 ...
- LeetCode 84. Largest Rectangle in Histogram 单调栈应用
LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...
- 刷题84. Largest Rectangle in Histogram
一.题目说明 题目84. Largest Rectangle in Histogram,给定n个非负整数(每个柱子宽度为1)形成柱状图,求该图的最大面积.题目难度是Hard! 二.我的解答 这是一个 ...
- 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...
- 【LeetCode】84. Largest Rectangle in Histogram
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- 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 ...
- 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 ...
- 84. Largest Rectangle in Histogram *HARD* -- 求柱状图中的最大矩形面积
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 ...
随机推荐
- Tracer使用
1.选择event List可以迅速完成操作,而选择simulation就会一步一步地执行操作,但是如果都点了下方的Delete删了记录的话,所有的机器都是该整个流程执行完毕的结果.
- LeetCode 3. 无重复字符的最长子串(Longest Substring Without Repeating Characters)
题目描述 给定一个字符串,找出不含有重复字符的最长子串的长度. 示例: 给定 "abcabcbb" ,没有重复字符的最长子串是 "abc" ,那么长度就是3. ...
- Daily Schedule
8.29~9.2周报: 完成Forward Path页面测试用例 2天 完成当前Return Path可执行控件的相关测试用例 1.5天 整理更新web自动化测试用例文档 1天 跟踪定位SNMP自动化 ...
- 如何使用EF?
方法1: 新建好项目之后 → 右击类库 → 新建项 → ADO.NET实体数据模型(在Visual C#项中) → 从数据库生成 → 选择你要映射的数据库的数据源(将 『是,在连接字符串中包含敏感数据 ...
- SAP MaxDB日常运维—启动、关闭、磁盘扩容
SAP MaxDB日常维护1.检查MaxDB状态,并启动su - pe0csccd /sapdb/SDB/db/bin./dbmcli -d SDB -u superdba,Mypassword db ...
- 1. 参数的传入和添加 argparse.ArgumentParser()
# Edit configuration 传入的参数使用的是--file_dir picture, 获取使用的是argv.file_dir import argparse, sys def parse ...
- 【转】C++ const成员变量和成员函数(常成员函数)
转:http://c.biancheng.net/view/2230.html 在类中,如果你不希望某些数据被修改,可以使用const关键字加以限定.const 可以用来修饰成员变量和成员函数. co ...
- samba安装应用实例-2
应用实例: 先安装samba软件,yum install -y samba1.需求:共享一个目录,使用用户名和密码登录才可以访问,要求可读可写.(1)首先打开samba配置文件/etc/samba/s ...
- 阶段3 3.SpringMVC·_01.SpringMVC概述及入门案例_03.入门程序之需求分析
- HttpRunnerManager(一)--安装
1.相关地址 (1)中文文档介绍:https://cn.httprunner.org/ (2)相关安装包下载地址:链接:https://pan.baidu.com/s/13SP1mFsNKrLK0sn ...