[Leetcode click Notes] largest rectangle in Histogram

Source: Internet
Author: User

GivenNNon-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 =10Unit.

 

For example,
Given Height =[2,1,5,6,2,3],
Return10.

 

Problem: I have been studying it online for a long time.

First, a stack is required to store each index in the stack. When the current element is greater than the element at the top of the stack or the stack is empty, the current element is added to the stack;

When the current element is smaller than the top element of the stack, the top element of the stack is popped up and the index is recorded on the top variable. There are two possibilities:

  • The stack is not empty, as shown in. From the top of the stack to the top of the stack (the current output stack) the height must be greater than or equal to the top (otherwise they should still be in the stack). There are (top-stack top index-1) items, while top ~ The height of the I section must be greater than or equal to the top height (otherwise, the pointer will not have the top just out of the stack and the pointer has been moved to I) (including top itself), so the maximum extension length of top to the left and right is (top-stack top index-1) + (I-top) = (I-stack top index-1), the maximum area generated is height [Top] * (I-stack top index-1 ).

  • The stack is empty, as shown in. It means that the top bar (the current outbound stack bar) can reach the left, and the right can reach the I, a total of I, the maximum area generated is height [Top] * I.

So when the element top out of the stack, the maximum area it can produce is height [Top] * (stack. isempty ()? I: i-stack.top ()-1 ).

To sum up the significance of the stack, for the entries at position I, the elements stored in the stack can be "extended" to the I, so they are smaller than I, you can use I to increase the maximum area of the rectangle generated by yourself, and those higher than I can no longer extend to the right through I, that is, the parts that can be extended to the right have been blocked by I, so we can calculate the distance between them to calculate the maximum area of the rectangle they can produce. For example, when I = 4, the red bar can no longer expand to the right through 4, and the expansion to the right is blocked by 4, so it has to go out of the stack, while the green bar is shorter than 4, it can still use 4 to increase the area of the rectangle it produces, so it does not need to exit the stack, because we have not yet determined its right boundary.

Finally, there are two trick points:

  • First, for example, after the traversal is complete, the green bars are still in the stack, so we need to add a zero-length entry at the end of all the bars, it can play out all the entries in the stack, and we get the area of the largest rectangle that can be produced by all the entries.
  • Second, after an element pops up the stack, the I pointer should remain unchanged, because it is possible that the element before it is shorter than the one pointed to by I, and the stack needs to be output.

The Code is as follows:

 1 public class Solution { 2     public int largestRectangleArea(int[] height) { 3         if(height == null || height.length == 0) 4             return 0; 5         Stack<Integer> index = new Stack<Integer>(); 6         int totalMax = 0; 7         ArrayList<Integer> newHeight = new ArrayList<Integer>(); 8         for(int i:height) newHeight.add(i); 9         newHeight.add(0);10         11         12         for(int i = 0;i < newHeight.size();i++){13             if(index.isEmpty() || newHeight.get(i) >= newHeight.get(index.peek()))14                 index.push(i);15             else{16                 int top = index.pop();17                 totalMax = Math.max(totalMax,newHeight.get(top) * (index.isEmpty()?i:i-top));18                 i--;19             }20         }21         22         return totalMax;23     }24 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.