Largest Rectangle in histogram

Source: Internet
Author: User

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 the where width of each bar is 1, given height = [2,1,5,6,2,3] .

The largest rectangle is shown in the shaded area, and which has an area = 10 unit.

Example

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

Analysis:

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

1  Public classSolution {2     /**3 * @param height:a list of integers4 * @return: the area of largest rectangle in the histogram5 * cnblogs.com/beiyeqingteng/6      */7      Public intLargestrectanglearea (int[] height) {8         if(Height = =NULL|| Height.length = =0)return 0;9         intMaxarea =0;Ten          for(inti =0; i < height.length; i++) { One             intMin =Height[i]; A              for(intj = i; J < Height.length; J + +) { -Min =math.min (min, height[j]); -Maxarea = Math.max ((j-i +1) *min, maxarea); the             } -         } -         returnMaxarea; -     } +}
View Code

Actually, we can decrease the complexity by using a 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-previou s index). Push the height and index to stacks.

(Note:it is better use another different example to walk through the steps, and you'll understand it better, you first Use a example in which all the heights is increasing.).


1  Public classSolution {2     /**3 * @param height:a list of integers4 * @return: the area of largest rectangle in the histogram5 * cnblogs.com/beiyeqingteng/6      */7     8      Public intLargestrectanglearea (int[] height) {9         if(Height = =NULL|| Height.length = =0)return 0;Ten          OneStack<integer> sheight =NewStack<integer>(); AStack<integer> Sindex =NewStack<integer>(); -         intMaxarea =0; -          the          for(inti =0; i < height.length; i++) { -             if(Sheight.isempty () | | height[i] >Sheight.peek ()) { - Sheight.push (Height[i]); - Sindex.push (i); +}Else if(Height[i] <Sheight.peek ()) { -                 intindex =-1; +                  while((!sheight.isempty ()) && Sheight.peek () >Height[i]) { Aindex =Sindex.pop (); atMaxarea = Math.max (Maxarea, (i-index) *Sheight.pop ()); -                 } - Sheight.push (Height[i]); - Sindex.push (index); -             } -         } in          -          while(!Sheight.isempty ()) { toMaxarea = Math.max (Maxarea, (Height.length-sindex.pop ()) *Sheight.pop ()); +         } -         returnMaxarea; the     } *}
View Code

Largest Rectangle in histogram

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.