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.

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 }

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.).

public class Solution {
  public int largestRectangleArea(int[] height) {
      if (height == null || height.length == 0) return 0;
      Stack<Item> items = new Stack<>();
      int maxArea = 0;
      for (int i = 0; i < height.length; i++) {
          if (items.isEmpty() || height[i] >= items.peek().height) {
              items.push(new Item(height[i], i));
          } else {
              Item item = null;
              while ((!items.isEmpty()) && height[i] < items.peek().height) {
                  item = items.pop();
                  maxArea = Math.max(maxArea, (i - item.index) * item.height);
              }
              items.push(new Item(height[i], item.index));
          }
      }
      
      while (!items.isEmpty()) {
          Item item = items.pop();
          maxArea = Math.max(maxArea, (height.length - item.index) * item.height);
      }
      return maxArea;
  }
}

class Item {
  int height;
  int index;
  public Item(int height, int index) {
      this.height = height;
      this.index = index;
  }
}

 

posted @ 2016-06-30 12:11  北叶青藤  阅读(272)  评论(0编辑  收藏  举报