"Leetcode" largest Rectangle in histogram

Source: Internet
Author: User

The topic information is as follows:
1, the title address is: https://leetcode.com/problems/largest-rectangle-in-histogram/
2. The title means:
Given a non-negative group height, which represents the height of the rectangle (where the rectangle width is 1), the maximum rectangular area can be found.
3, give the example is: height = [2,1,5,6,2,3]. The output is as follows: 10.

Then the following is the text, the basis of the algorithm (in the example above):

1, the first 2 is nothing to say, the area is bound to be 2.
2, when the pointer points to height[1], the area has 1 (that is, itself) and 2 (such as) two possible.

3, when the pointer points to height[2], as shown, there are 3 kinds of area possible.

4, when the pointer points to height[3], such as, there are 4 possible (the picture is less h[1]-[3] of the combination)

Contrast 3, 4 I found a useful rule:
H[2], all areas of the area can be added to the area of h[3].
That is, if the height of the pointer (such as H[2]=5) is followed by a higher or equal height (such as h[3]=6), then the maximum area cannot be the height of the pointer and the preceding combination.


Use the following picture to describe the above law, more appropriate. If you want to find the largest area in the, there must be a height of 6 of the participation.

Another paragraph:


What do we do when we meet the above arrangement?
Look at the above introduction, you will say, direct 1 5 6 in the combination to find the largest, then 5 6 2 to find the largest area, and finally look at the area of 1*4.
Seems a bit of a problem, such as 1 5 6 in combination, there may be 1 1 1 such a matrix, then why not directly say the matrix pulled to 2, that is 1 1 1 1 = 4 of the area Wow?
So in summary we can get this combination method:

The pointer moves backwards from 1, keeps a record of the information until the pointer points to 2, because 2<6, at this point, starts to calculate the front height 5, 6 of the area combination, why not calculate 1, because 1<2, if the maximum area combination contains 1, then must contain 2, such as the red box. Finally we know that the height after 1 is greater than 2, then the area = (2 subscript-1 Subscript-1) * * height.
Finally, attach the code:

 Public intLargestrectanglearea (int[] height) {if(height = = NULL | | height.length = =0)return 0;intlen = height.length; Stack<integer>Stack=NewStack<integer> ();intMax =0; for(intI=0; i<len; i++) {if(Stack. Empty () | | Height[i] >= height[Stack. Peek ()]) {//will be greater than or equal to the top of the stack into the stack, then the middle is a non-decreasing sequence                Stack. push (i); }Else{//height height less than the top of the stack                intleft =0; while(!Stack. Empty () && Height[i] < height[Stack. Peek ()]) {//Until the top of the stack is less than the current heightleft =Stack. Pop ();intcur = height[left] * (Stack. Empty ()? i:i-Stack. Peek ()-1);//In the stack is incremented, the stack is decremented, that is, the current sequence height * The number of stacks, is the current area                    if(Max < cur)                    {max = cur; }                }Stack. push (i); }        }intleft =0; while(!Stack. empty ()) {//This is used to process incremental or ascending subsequence such as [1 2 3 4 5 6 7 8]left =Stack. Pop ();intcur = height[left] * (Stack. Empty ()? len:len-Stack. Peek ()-1);if(Max < cur) max = cur; }returnMax }

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Leetcode" 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.