首页 > 其他 > 详细

Largest Rectangle in Histogram

时间:2016-06-30 14:09:15      阅读:192      评论:0      收藏:0      [点我收藏+]

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.

Example

Given height = [2,1,5,6,2,3], return 10.

分析:

When solving this problem, the straightforward approach is to consider all possible cases, i.e., we begin with the ith rectangle, and then enumerate the remaining rectangles after the ith rectangle, each time, we need to compare the area with the maximum area. The time complexity is O(n^2).

技术分享
 1 public class Solution {
 2     /**
 3      * @param height: A list of integer
 4      * @return: The area of largest rectangle in the histogram
 5      * cnblogs.com/beiyeqingteng/
 6      */
 7     public int largestRectangleArea(int[] height) {
 8         if (height == null || height.length == 0) return 0;
 9         int maxArea = 0;
10         for (int i = 0; i < height.length; i++) {
11             int min = height[i];
12             for (int j = i; j < height.length; j++) {
13                 min = Math.min(min, height[j]);
14                 maxArea = Math.max((j - i + 1) * min, maxArea);
15             }
16         }
17         return maxArea;
18     }
19 }
View Code

 

Actually, we can decrease the complexity by using stack to keep track of the height and start indexes. Compare the current height with previous one.

Case 1: current > previous (top of height stack)
Push current height and index as candidate rectangle start position.

Case 2: current = previous
Ignore.

Case 3: current < previous
Need keep popping out previous heights, and compute the candidate rectangle with height and width (current index - previous index). Push the height and index to stacks.

(Note: it is better use another different example to walk through the steps, and you will understand it better, you first use an example in which all the heights are increasing.).


技术分享

 

技术分享
 1 public class Solution {
 2     /**
 3      * @param height: A list of integer
 4      * @return: The area of largest rectangle in the histogram
 5      * cnblogs.com/beiyeqingteng/
 6      */
 7     
 8     public int largestRectangleArea(int[] height) {
 9         if (height == null || height.length == 0) return 0;
10         
11         Stack<Integer> sHeight = new Stack<Integer>();
12         Stack<Integer> sIndex = new Stack<Integer>();
13         int maxArea = 0;
14         
15         for (int i = 0; i < height.length; i++) {
16             if (sHeight.isEmpty() || height[i] > sHeight.peek()) {
17                 sHeight.push(height[i]);
18                 sIndex.push(i);
19             } else if (height[i] < sHeight.peek()) {
20                 int index = -1;
21                 while ((!sHeight.isEmpty()) && sHeight.peek() > height[i]) {
22                     index = sIndex.pop();
23                     maxArea = Math.max(maxArea, (i - index) * sHeight.pop());
24                 }
25                 sHeight.push(height[i]);
26                 sIndex.push(index);
27             }
28         }
29         
30         while (!sHeight.isEmpty()) {
31             maxArea = Math.max(maxArea, (height.length - sIndex.pop()) * sHeight.pop());
32         }
33         return maxArea;
34     }
35 }
View Code

 

Largest Rectangle in Histogram

原文:http://www.cnblogs.com/beiyeqingteng/p/5629562.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!