# Leetcode largest Rectangle in histogram solution two

Source: Internet
Author: User

The previous article talked about a solution to the problem. Later, a better solution was found.

The first step is to consider an ascending sequence, such as 1,2,3,4,5. Then its maximum rectangle is obviously there are 5 possibilities, namely

1*5,2*4,3*3,4*2,1*5. So the largest rectangle is 9. Then obviously it can't be ascending sequence.

It is processed according to the following rules.

Stack stack and pending array A[n]

1. If the stack is empty, then a[i] into the stack.

2. If A[i]>=stack.peek (), then a[i] into the stack

3. If A[i]<stack.peek (), then stack pops up until A[i]>=stack.peek (). For all pop-up values, calculate their area.

After performing the eject operation, press the a[i with the same number of POPs]

4. After traversing A[i], the elements in the stack are processed.

Give an example:

Like 2,1,5,6,2,3.

(1) 2 into the stack. S={2}, result = 0

(2) 1:2 Small, does not meet the ascending condition, so 2 pops up and records the current result as 2*1=2.

Replace 2 with 1 to re-enter the stack. s={1,1}, result = 2

(3) 5:1 large, meet the ascending conditions, into the stack. S={1,1,5},result = 2

(4) 6:5 large, meet the ascending conditions, into the stack. S={1,1,5,6},result = 2

(5) 2:6 Small, does not meet the ascending condition, so 6 pops up and records the current result as 6*1=6. S={1,1,5},result = 6

2:5 small, does not meet the ascending condition, so 5 pops up and records the current result as 5*2=10 (because the 5,6 that have popped up is ascending). S={1,1},result = 10

2:1 Large, replace the pop-up 5,6 with the 2 re-enter stack. S={1,1,2,2,2},result = 10

(6) 3:2 large, meet the ascending conditions, into the stack. S={1,1,2,2,2,3},result = 10

The stack is built to meet ascending conditions, so you get the above Max (height[i]* (size-i)) =max{3*1, 2*2, 2*3, 2*4, 1*5, 1*6}=8<10, in ascending order of processing.

In summary, result=10

Careful analysis, in fact, this solution means that if it is ascending, then directly calculate its area, encountered a drop point, then actually

This point is a dent, and the rectangle that contains the point is only as high as the value of the point. The goal of the final statistical stack is to count the drop points.

Give the Code

`ImportJava.util.Stack; Public classSolution2 {/**     * @paramargs*/     Public intLargestrectanglearea (int[] height) {Stack<Integer> stack=NewStack<integer>(); intMaxarea=0;  for(inth:height)            {System.out.println (stack); if(Stack.empty () | | Stack.peek () <h) {Stack.push (h); }            Else            {                intI=1;  while(!stack.empty () &&stack.peek () >h) {inttmp=Stack.pop (); if(tmp*i>Maxarea) Maxarea=tmp*i; I++; }                 for(intj=0;j<i;j++) {Stack.push (h);        }}} System.out.println (stack); intI=1; if(Stack.empty ())returnMaxarea; intprevious=Stack.pop ();  while(!Stack.empty ()) {            if(previous!=Stack.peek ()) {System.out.println (previous*i+ "DDD"); if(previous*i>Maxarea) Maxarea=previous*i; I++; Previous=Stack.pop (); }            Else{i++;            Stack.pop (); }        }        if(previous*i>Maxarea) Maxarea=previous*i; returnMaxarea; }     Public Static voidMain (string[] args) {//TODO auto-generated Method Stub            int[]a={1,2,2}; System.out.println (NewSolution2 (). Largestrectanglearea (a)); }}`

Leetcode largest Rectangle in histogram solution two

Related Keywords:

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.