Leetcode notes: Largest Rectangle in histogram

Source: Internet
Author: User

I. Title Description

Given n non-negative integers representing the histogram's bar height where the width of each bar are 1, find the area of L Argest 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.

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

Two. Topic analysis

See this topic, the first time should be introduced is the histogram of the maximum height of the rectangle must be equal to the height of a column.

Therefore, it is easy to think of traversing the array, for a column, to the left and right sides of the height[index] expansion, to see the height of the current column can contain a large rectangular area, the height of the second column 1 , scanning its left and right elements found all elements are greater than 1 , so the height of the width is 6 , Get the area 1 * 6 = 6 . The maximum area that is constantly being updated, and the value is returned at the end. The time complexity of this method is O(n^2) that it will time out.

The correct and efficient method is a widely discussed online method, the use of stacks to implement the algorithm, but the algorithm is not easy to think immediately. Here is a simple example to illustrate this algorithm:

Figure, the height = [5,6,7,8,3] feature is that except for the last one, the front all keeps incrementing, and the height of the last column is less than the height of all the previous columns.

For the histogram of this feature, we know that except for the last one, the height of the second column from the first to the penultimate is rising, and if you use each bar height as the height of the rectangle, then the width of the rectangle can be calculated directly: Use 5 as a height can be used as the top four columns composed 4*5rectangle, in the same vein using a height 6 of the column can be composed 3*6 of a rectangle ... Therefore, height you can calculate the maximum area, which is the time complexity, by traversing only once O(n) .

In the reference post, the histogram of this feature is called the "Crest Chart".

The following is a detailed procedure for this algorithm:

1. The end of the height array 0 is added, its function is to obtain a histogram that conforms to the above law.

2. Define a stack k, traversing the array height , if height[index] greater than stack.top() , into the stack, and vice versa, until the top of the stack is less than the element height[index] . Since the height of these elements in the stack is incremented, we can sequentially find the rectangular area in these columns and update its maximum value.

3. Repeat the process until the last element of value 0 is traversed, all the elements in the stack are ejected and the last area is computed, and the maximum value of the area is returned.

Note that the stack is not the size of the height element, but height the index, the benefit of doing so does not affect the width of the calculation, the current traversed index value-the current stack top index value-1 = the width of the current rectangle.

Three. Sample code

classsolution{ Public:intLargestrectanglearea ( vector<int>&height) {if(height.size () = =0)return 0; Height.push_back (0);intMaxhist =0;//Storage maximum rectangular area         Stack<int>K//Use stacks to store the index of height         for(intindex =0; Index < height.size (); ++index) {if(K.empty () | | height[k.top ()] < Height[index]) K.push (index);Else{inttemp = K.top (); K.pop ();//Local area calculation, width is the distance from index K.top () of the current index to the top of the stack                intLocalarea = height[temp] * (K.empty ()? Index: (Index-k.top ()-1));if(Localarea > Maxhist)                Maxhist = Localarea;            --index; }        }returnMaxhist; }};

Four. Summary

This topic requires traversing each column and treat it as a rectangular height, to find out the histogram can form the maximum area of the rectangle, but it through the clever use of the stack, the whole height into a group of "wave map" to solve, to achieve the O (n) time complexity, worth in-depth study!

Reference Links:

Http://www.cnblogs.com/felixfang/p/3676193.html
Http://www.cnblogs.com/avril/archive/2013/08/24/3278873.html

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

Leetcode notes: 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.