Leetcode 84. histogram largest rectangle

Leetcode 84. histogram largest rectangle

Subject description:

Given n non-negative integer that represents the height of each column in the histogram. Each column adjacent to each other, and a width of 1.

In seeking this histogram, the maximum area can be outlined rectangle.

 

The above is an example of the histogram, wherein the width of each column is 1, given that the height of [2,1,5,6,2,3].

img

 

The shaded portion is the maximum that can be outlined by a rectangular area, an area of ​​10 units.

 

Example:

Input: [2,1,5,6,2,3] Output: 10

Source: stay button (LeetCode) link: https://leetcode-cn.com/problems/largest-rectangle-in-histogram

Monotonous stack:

We can see a histogram of the rectangular area to be the biggest, the need to make possible continuous Rectangle, and a minimum height is higher. A bit like a bucket theory, there is always the lowest piece of board decided to install water bucket.

Well, since the need to do monotonous stack, we must first consider in the end with increasing stack, or by decreasing the stack to do. We think, ah, incrementing the stack is to maintain the order of increasing, when faced with a number less than the top element of the process to start, while decreasing the stack on the contrary, to maintain the order of decreasing, when the number is greater than the top element of the encounter begins processing. Then according to the characteristics of this question, we need the board in order from high to low processing board, the highest first processing board, a width of 1, and then some of the shorter side board processing, this time length of 2, because before high the board may rectangles short board, so we need an increasing stack, when faced with large numbers directly into the stack, and when faced with a number less than the top element, we must remove the top element is processed, then taken the order is from the high board into a short board, and thus a smaller number encountered only a trigger , and now represents the need to begin to calculate the area of a rectangle.

Finally, in order to make a board is also treated with a small Trick herein, the height of the array at the end face plus a 0, so the original last board may be processed. We can not put monotonous stack height, but need to put coordinates. Since we first removed the stack top board, then you can first calculate the length of the rectangular area 1, and then removing a board, then the length calculated according to the height of a rectangular area of ​​the short board 2, and so on, known digital becomes greater than the top element, push again.

class Solution {
   public int largestRectangleArea(int[] heights) {
       int res=0;
       //扩展数组使最后一位为0
       int[] height=Arrays.copyOf(heights,heights.length+1);
       height[height.length-1]=0;
       LinkedList<Integer> s=new LinkedList<>();
       for (int i=0;i<height.length;I ++) { // If not incremented trigger popping the while ( ! S. isEmpty () && [height I] <= height [ S. PEEK ()]) { int H = height [ S. POP ()] ; // take the stack height position int W = S. isEmpty () ? I: I - S. PEEK () - . 1; // width of the current required to be calculated //is.peek()-1 means popping process, the position of the traverse to the maximum achievable width of the rectangle area must be marked and the distance between the current I under // when the pop-up time before the stack is empty, to consider the position. Height positions of the pop-up before a certain current is higher than the bottom of the stack, the width of all direct access to the current index I. Examples of this case is [2,1,2] RES = the Math. Max ( RES, W *
           
           
               
               
               
               
               H); // updates the maximum
          } S. Push ( I);       } return RES;   } }
           

       

 

Guess you like

Origin www.cnblogs.com/pihaochen/p/11518787.html