【直方图的最大面积】Largest Rectangle in Histogram
2014-04-05 17:30
330 查看
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 =
The largest rectangle is shown in the shaded area, which has area =
For example,
Given height =
return
思路参考:http://www.cnblogs.com/lichen782/p/leetcode_Largest_Rectangle_in_Histogram.html
解法二:穷举,时间复杂度O(n2)
转自:http://leetcodenotes.wordpress.com/2013/07/21/largest-rectangle-in-histogram/
穷举两个边界,但不是完全穷举,这里可以省掉一部分右右边界。对于i,如果A[i] <= A[i + 1],即当前递增,则无论对于哪个左边界,选i+1作为右边界都比选i作为右边界合适(宽本来就多一个,然后高还更高,所以肯定选右边的面积大)。所以就算i+1作为右边界的所有左边界配对就行,没必要算i作为右边界的了。所以递推下去,只有A[i] > A[i + 1]的时候,在递减,说明拿A[i]和拿A[i
+ 1]不一定谁大了,这时候算一下A[i]作为右边界的左边界所有可能,就是一个local peak。这题还有一个O(n)的解法,但是暂时懒的看了。下次再看。
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 =
10unit.
For example,
Given height =
[2,1,5,6,2,3],
return
10.
思路参考:http://www.cnblogs.com/lichen782/p/leetcode_Largest_Rectangle_in_Histogram.html
public class Solution { public int largestRectangleArea(int[] height) { Stack<Integer> s = new Stack<Integer>(); int [] h = new int[height.length + 1]; h = Arrays.copyOf(height, height.length + 1); int i = 0, area = 0; while(i < h.length){ if(s.isEmpty() || h[i] >= h[s.peek()]){ s.push(i++); } else{ int t = s.pop(); int tArea = h[t] * (s.isEmpty() ? i : i-s.peek()-1); area = Math.max(tArea, area); } } return area; } }
解法二:穷举,时间复杂度O(n2)
转自:http://leetcodenotes.wordpress.com/2013/07/21/largest-rectangle-in-histogram/
穷举两个边界,但不是完全穷举,这里可以省掉一部分右右边界。对于i,如果A[i] <= A[i + 1],即当前递增,则无论对于哪个左边界,选i+1作为右边界都比选i作为右边界合适(宽本来就多一个,然后高还更高,所以肯定选右边的面积大)。所以就算i+1作为右边界的所有左边界配对就行,没必要算i作为右边界的了。所以递推下去,只有A[i] > A[i + 1]的时候,在递减,说明拿A[i]和拿A[i
+ 1]不一定谁大了,这时候算一下A[i]作为右边界的左边界所有可能,就是一个local peak。这题还有一个O(n)的解法,但是暂时懒的看了。下次再看。
public int largestRectangleArea(int[] height) { int res = 0; for (int i = height.length - 1; i >= 0; i--) { if (i == height.length - 1 || height[i] > height[i + 1]) { //A[i] is a local peak int min = Integer.MAX_VALUE; for (int j = i; j >= 0; j--) { min = Math.min(min, height[j]); res = Math.max(res, (i - j + 1) * min); } } } return res; }
相关文章推荐
- LeetCode_直方图最大面积(Largest Rectangle in Histogram)
- Largest Rectangle in Histogram (直方图中最大面积) 【leetcode】
- LeetCode | Largest Rectangle in Histogram(直方图围城的最大矩形面积)
- ACM-直方图最大面积-Largest Rectangle in Histogram
- 4.1.3LeetCode: Largest Rectangle in Histogram(直方图最大面积)
- [LeetCode]—Largest Rectangle in Histogram 求直方图最大填充矩形面积
- LeetCode: Largest Rectangle in Histogram(直方图最大面积)
- LeetCode: Largest Rectangle in Histogram(直方图最大面积)
- 柱状图内最大的矩形面积 Largest Rectangle in Histogram @LeetCode
- HDU 1056 Largest Rectangle in a Histogram(dp)(求最大的矩形面积)
- 直方图最大矩形问题(Largest Rectangle in Histogram)
- POJ 2559 Largest Rectangle in a Histogram(单调栈维护最大矩形面积)
- HDU 1506 Largest Rectangle in a Histogram (最大子矩形面积)
- Largest Rectangle in a Histogram(动态规划找最大矩形面积)
- HDU 1506 Largest Rectangle in a Histogram【矩阵最大面积】
- [Leetcode] largest rectangle in histogram 直方图中最大的矩形
- LeetCode OJ 之 Largest Rectangle in Histogram (直方图中的最大矩形)
- 解题报告:LeetCode Largest Rectangle in Histogram(计算最大矩形面积)
- Hdu 1506 Largest Rectangle in a Histogram (DP求最大长方形面积)
- 栈——largest-rectangle-in-histogram求柱形图中的最大矩形面积