【LEETCODE】模拟面试-84-Largest Rectangle in Histogram
题目:
https://leetcode.com/problems/largest-rectangle-in-histogram/
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.
For example,Given heights = [2,1,5,6,2,3], return 10.
分析:
This question is to get a maximum area of largest rectangle in the histogram.
Input is an array. Output is an int;
The brute force method is to scan its left side and right side for each bar, till the left boundary and right boundary which are lower than this bar. Then the area would be its height times index of right boundary minus left boundary. After all the bars are calculated, we can get the max area.
Another method is not to calculate the repeated work during the scanning. For example, when we scan the right side of bar_index=2( height=5 ), its right boundary is bar_index=4( height=2 ). And when we start to scan the left boundary for bar_index=4( height=2 ), in fact, we should already know bar_index=2( height=5 ) can cover this bar, so it will save time if we reduce the duplicate scanning, and store them in a data structure.
Here, we try to use stack
to store such information.
- We scan from left to right.
- We add bar's index where might be the left boundary of its following bars, of course including current bar itself.
- We stop adding index as soon as we meet a bar which is lower then the height of top in the stack, since it means we have found the right boundary of the newest bar in the stack.
- Then we can start to calculate the possible area which are covered by the histogram determined by the stack.
- The formula is easy: (right boundary index - left boundary index) * current height reminder: the right boundary index is where the stack is stopped. the nearest left boundary is just the one bar in front of current bar. the height of a bar can be calculated in the bar is which is higher than both of its left AND right boundary.
For detail explanation can be found in code.
Java
public class Solution {
public int largestRectangleArea(int[] heights){
if ( heights == null || heights.length == 0 )
return 0;
Deque<Integer> stack = new LinkedList<Integer>(); //store index
int max = 0;
for ( int i = 0; i <= heights.length; i++ ){
int curVal = (i == heights.length) ? 0 : heights[i]; //如果i到array的外面了,curVal=0,否则就为当前index装的高度
while ( !stack.isEmpty() && heights[stack.peekLast()] >= curVal ){ //curVal 小于heights[stack.peekLast()],说明cur是最顶端一点的右边界
//如果 curVal 大于stack.peekLast(),说明这是cur的一个左边界
//stack里每个元素都是后一个元素的左边界,停止add的时候说明碰到了右边界
int height = heights[stack.pollLast()]; //pollLast get height(index) AND REMOVE index from stack
int leftBound = stack.isEmpty() ? 0 : (stack.peekLast() + 1); //注意 why +1: since pollLast() removed
int rightBound = i;
max = Math.max( max, height * (rightBound - leftBound) ); //用公式计算面积
}
stack.addLast(i); //stack=[1,4]时,说明 1处比4处值小,但是2处被弹出去了,说明4处曾经是2处的右边界,4处相当于凹心
}
return max;
}
}
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法