1 暴力法,TLE
2 很巧妙的用了两个stack,一个stack保存高度,另一个保存index
遍历height数组,
1 如果stack为空或者当前元素的高度大于height stack顶元素(前一个元素)的高度 => 把当前高度额index分别添加到两个stack顶
2 如果当前元素的高度等于height stack顶元素的高度 => 什么都不做
3如果当前元素的高度小于height stack顶元素的高度 => 持续弹栈直到当前元素的高度大于栈顶元素,并每次都计算面积(height * (current index - popped index)),保存最大面积。最后把当前高度和最后一个弹出的index(非当前index) 重新压入栈中
4 处理完height数组,如果stack非空,则一一弹栈并计算面积。
http://www.youtube.com/watch?v=E5C5W6waHlo
package Level5;
import java.util.Stack;
/**
* 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.
http://www.leetcode.com/wp-content/uploads/2012/04/histogram.png
Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].
http://www.leetcode.com/wp-content/uploads/2012/04/histogram_area.png
The largest rectangle is shown in the shaded area, which has area = 10 unit.
For example,
Given height = [2,1,5,6,2,3],
return 10.
*
*/
public class S84 {
public static void main(String[] args) {
int[] height = {2,1,2};
System.out.println(largestRectangleArea(height));
}
public static int largestRectangleArea(int[] height) {
if(height.length == 0){
return 0;
}
Stack<Integer> heightStack = new Stack<Integer>();
Stack<Integer> indexStack = new Stack<Integer>();
heightStack.push(height[0]);
indexStack.push(0);
int max = height[0];
for(int i=1; i<height.length; i++){
/*
#1: current Larger than previous (top of height stack)
Push current height & index as candidate rectangle start position
*/
if(heightStack.isEmpty() || height[i] > heightStack.peek()){
heightStack.push(height[i]);
indexStack.push(i);
}
/*
#3: current is less than previous
Need keep popping out previous heights, and compute the candidate rectangle with height and width (current index MINUS previous index)
Push the height and index (appropriate position) to stacks
*/
else if(height[i] < heightStack.peek()){
int lastIndex = 0; // used to keep track of last index which will be replacing the current index for current height inserting
while(!heightStack.isEmpty() && height[i]<heightStack.peek()){
lastIndex = indexStack.pop();
int h = heightStack.pop();
int w = i - lastIndex;
max = Math.max(max, h*w);
}
heightStack.push(height[i]);
indexStack.push(lastIndex); // Here should be lastIndex, one example: [2,1,2] => if lastIndex:3, if i:2
}
}
// deal with remaining elements in the stack
while(!heightStack.isEmpty()){
int lastIndex = indexStack.pop();
int h = heightStack.pop();
int w = height.length - lastIndex;
max = Math.max(max, h*w);
}
return max;
}
// TLE
public static int largestRectangleArea2(int[] height) {
int max = 0;
int len = height.length;
for(int i=0; i<len; i++){
int minHeight = height[i];
for(int j=i; j<len; j++){
minHeight = Math.min(minHeight, height[j]);
max = Math.max(max, (j-i+1)*minHeight);
}
}
return max;
}
}
分享到:
相关推荐
LeetCode题目Largest Rectangle in Histogram 解答
LintCode - 122. Largest Rectangle in Histogram(直方图最大矩形覆盖)(单调栈)题目链接题目解析主要是运用单调栈(单
http://blog.csdn.net/two_water/article/details/53004027 LeetCode_直方图最大面积(Largest Rectangle in Histogram)
力扣最大面积直方图中最大的矩形 给定 n 个非负整数表示直方图的条形高度,其中每个条形的宽度为 1,求直方图中最大矩形的面积。 上面是一个直方图,其中每个条的宽度为 1,给定高度 = [2,1,5,6,2,3]。 最大的矩形...
1. Introduction ... Largest Rectangle in Histogram ix. Maximal Rectangle x. Palindrome Number xi. Search a 2D Matrix xii. Search for a Range xiii. Search Insert Position xiv. Find Peak Element
421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [C++](./C++/maximum-xor-of-two-numbers-in-an-array.cpp) [Python](./Python/...
leetcode 316 LeetCode Summary Exclusive Time of Functions: 栈 Friend Circles:DFS Print Binary Tree:二叉树 Maximal Square:DP Maximal Rectangle:单调栈(Histogram变形) Largest Rectangle in Histogram:...
c++经典程序 北邮c++实验课老师留的作业
实现利用Rectangle输出一个矩形的周长和面积
已知的矩形类(Rectangle),现在希望增强它的功能,使得这个具有增强功能的矩形类能够拥有通过getCenter()获得Point和setCenter(intx,inty)的功能
定义一个名为rectangle 的矩形类,其属性数据为矩形左上角和右上角的点的坐标能计算矩形的面积
C#画柱状图,可以使用的namespace 柱状图 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Graphics g ...
1.建立一个形状类Shape作为基类,派生出圆类Circle和矩形类Rectangle,求出面积并获取相关信息。 具体要求如下: (1)形状类Shape (a)保护数据成员 double x,y:对于不同的形状,x和y表示不同的含义,如对于圆,...
C#-矩形-Rectangle
java 实验 继承与多态rectAngle 定义矩形类,用户输入矩形的长与宽,程序计算其面积和周长;派生子类正方形类,定义一个接口Printable源代码
求解不规则图形的内切矩形,已面积最大为依据
设计一个矩形类Rectangle(Java)
largest-rectangle-in-histogram: 最长递增子序列: 最长公共自序列: Something about: best time to buy and sell stack: 单链表中的环,两个单链表的公共点。 populating-next-right-pointers-in-each-node-ii: ...
不会LeetCode_492--构造矩形 对于 Web 开发人员来说,了解如何设计网页的大小非常重要。 因此,给定一个特定的矩形网页区域,您现在的工作是设计一个矩形网页,其长度 L 和宽度 W 满足以下要求: 您设计的矩形网页的...
矩形面积计算,c++实现,rectangle的大小