【leetcode】 Largest Rectangle in Histogram
来源:互联网 发布:三菱plc编程教学视频 编辑:程序博客网 时间:2024/06/03 19:37
from : 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 height = [2,1,5,6,2,3]
,
return 10
.
按递增的顺序,找到最右边的一个递增位置,然后从这个位置往前逐个检查,更新最大面积。
时间复杂度为O(n^2)。
public class Solution { public int largestRectangleArea(int[] height) { int area = 0; for (int i = 0; i < height.length; i++) { for (int k = i + 1; k < height.length; k++) { if (height[k] < height[k - 1]) { i = k - 1; break; } else { i = k; } } int lowest = height[i]; for (int j = i; j >= 0; j--) { if (height[j] < lowest) { lowest = height[j]; } int currArea = (i - j + 1) * lowest; if (currArea > area) { area = currArea; } } } return area; }}
思路2:
用两个栈,一个表示当前的高度,一个表示当前的下标。
heightStack 和 indexStack;表示当前递增的元素下标和值的对应关系。也许有人疑问,为什么不直接存下标。看上面的图,对于Y=5这个点,它对应的下标最后会被更新为1,即Y=7的位置,在Y=4这个点,其面积为(4-1+1)*4,如果Y=5的下标不是1,而是其本身,那么Y=4可以延伸出的面积就少了。
相当于到Y=4这个点的时候,栈中表示的图形变为途中黑色区域:
这么说可能看不明白,看来代码和后面(1),(2)的解释应该会明白了。
1.在递增或者相等的情形下,入栈;在遇到减少的时候出栈计算。逐个计算当前面积并更新最大面积。
2.最后,将剩余栈中元素处理一下。
注意下面代码中的(1)和(2)处。
public class Solution { public int largestRectangleArea(int[] height) {int area = 0 , len = height.length;Stack<Integer> heightStack = new Stack<Integer>();Stack<Integer> indexStack = new Stack<Integer>();for (int i = 0; i < len; i++) {if (heightStack.empty() || heightStack.peek() <= height[i]) {heightStack.push(height[i]);indexStack.push(i);} else if (heightStack.peek() > height[i]) {int j = 0;while (!heightStack.empty() && heightStack.peek() > height[i]) {j = indexStack.pop();int currArea = (i - j) * heightStack.pop();if (currArea > area) {area = currArea;}}heightStack.push(height[i]); // (1)indexStack.push(j); // (2)}}while (!heightStack.empty()) {int currArea = (len - indexStack.pop())* heightStack.pop();if (currArea > area) {area = currArea;}}return area;}}
(1)和(2)表示:
j此时指向第一个比height[i]大的元素;对于新的栈中情况,此时要将height[i]和j对应起来,这样两个栈才表示尚未完成面积计算的元素。如果不明白,可以用例子[2,1,3]模拟一下,简单说明:
栈的情况如下:
1.
heightStack :2
indexStack :0
2.
heightStack :1
indexStack :0
3.
heightStack :1 1
indexStack :0 1
4.
heightStack :1 1 3
indexStack :0 1 2
其实,用一个栈,加上自定义的结点,可以更快更好处理。代码如下:
public class Solution { class Node { int i; int h; public Node(int i, int h) { this.i = i; this.h = h; } } public int largestRectangleArea(int[] height) {int area = 0, len=height.length;Stack<Node> lines = new Stack<Node>();for(int i=0; i<len; ++i) { if(lines.isEmpty() || lines.peek().h <= height[i]) { lines.add(new Node(i, height[i])); } else { int k = 0, h = height[i]; while(!lines.isEmpty() && lines.peek().h > h) { Node node = lines.pop(); k = node.i; int cur = (i-k)*node.h; if(cur > area) { area = cur; } } lines.add(new Node(k, h)); }}// last clearwhile(!lines.isEmpty()) { Node node = lines.pop(); int cur = (len-node.i)*node.h; if(cur > area) { area = cur; }}return area;}}
思路3:
基本跟上一个思路一致,区别在于只用一个栈放下标。
思路2中,下标主要用来计算宽度,那么这里不去对应高度和下标,如何做到?
这个栈存放的是当前递增height数组的子数组的下标,注意,自增数组不需要连续;其实思路二也是。也就是[1,2,5,4]这个数组的子数组可是是[1,2,4]。这个栈中的子数组是递增的。这个题目的过程一直是找到当前(当前指数组下标为i之前)最后一个递增子数组,在i之前的子数组,找到其中的最大面积。按照上面的例子,当i=4,前栈的状态时<0,2,3>。那么看下面(*)处的代码,可以知道栈顶元素对应的高度是其高度,这个高度对应的矩形的宽度则是栈中下一个元素(即下标)+1与当前栈顶元素的位置的举例。具体还是看代码。
//(*)中,stack空表示指向了下标为-1的地方,那么i-(-1)-1 = i。 所以计算如下。
public class Solution { public int largestRectangleArea(int[] height) {int area = 0, len=height.length;Stack<Integer> stack = new Stack<Integer>();for (int i = 0; i < len; ++i) {if (stack.empty() || height[stack.peek()] < height[i]) {stack.push(i);} else {int start = stack.pop();int width = stack.empty() ? i : i - stack.peek() - 1;area = Math.max(area, height[start] * width);--i;}}while (!stack.empty()) {int start = stack.pop();int width = stack.empty() ? len : len- stack.peek() - 1;//(*)area = Math.max(area, height[start] * width);}return area;}}
- LeetCode: Largest Rectangle in Histogram
- LeetCode Largest Rectangle in Histogram
- LeetCode: Largest Rectangle in Histogram
- [Leetcode] Largest Rectangle in Histogram
- [LeetCode] Largest Rectangle in Histogram
- [Leetcode] Largest Rectangle in Histogram
- [leetcode] Largest Rectangle in Histogram
- [LeetCode]Largest Rectangle in Histogram
- Leetcode: Largest Rectangle in Histogram
- [leetcode]Largest Rectangle in Histogram
- Leetcode:Largest Rectangle in Histogram
- LeetCode-Largest Rectangle in Histogram
- LeetCode Largest Rectangle in Histogram
- [LeetCode] Largest Rectangle in Histogram
- leetcode Largest Rectangle in Histogram
- [LeetCode] Largest Rectangle in Histogram
- leetcode Largest Rectangle in Histogram
- <Leetcode>Largest Rectangle in Histogram
- 浏览器向服务器发送请求报文
- 手机开发之H5+规范的原生UI警示框
- 【黑马程序员】Java笔记--集合框架
- 各种创建单例模式的优缺点
- Backbone.js快速入门(一)
- 【leetcode】 Largest Rectangle in Histogram
- Swift学习笔记之基本运算符(一)
- 在64位ubuntu14.04上安装校园客户端iNodeClient
- 从Sql Server通过HTTP推送数据到网页
- C语言的选择语句
- jsonp不能post!
- 当webview遇到360wifi
- 谈谈对Spring IOC的理解
- 3414POJ