[Leetcode] Largest Rectangle in histogram histogram with largest rectangle

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.

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

This problem let straight square diagram of the largest rectangle, just start to see the extreme problem to use DP to do, but can't think of recursion, had to give up. This problem if using brute force search method estimate certainly can't pass OJ, but I also did not come up with the good optimization method, in the net searched the Netizen Water Fish's blog, found that he came up with a good optimization method, is to traverse the array, each find a local peak, and then walk forward all the values, calculate the common rectangular area, Keep the maximum value each time, the code is as follows:

//Pruning optimizeclassSolution { Public:    intLargestrectanglearea (vector<int> &height) {        intres =0;  for(inti =0; I < height.size (); ++i) {if(i +1< Height.size () && height[i] <= height[i +1]) {                Continue; }            intMinH =Height[i];  for(intj = i; J >=0; --j) {MinH=min (MinH, height[j]); intArea = MinH * (i-j +1); Res=Max (res, area); }        }        returnRes; }};

Later on the internet to find a more popular solution, is to use the stack to solve, can be seen in the lab small paper stickers off-campus blog, but after careful study, its core ideas with the above kind of pruning method has the same wonderful, here to maintain a stack, used to save the increment sequence, equivalent to the method of finding local peak, When the current value is less than the top value of the stack, remove the top element of the stack, and then calculate the current rectangle area, and then compare the current value and the new stack top value size, if the stack top value is large, then remove the top of the stack, calculate the area of the common rectangular area, according to this analogy, can get the maximum rectangle The time complexity of the two solutions depends heavily on the input array, and in the worst case, for example, the input array is a descending sequence, then the complexity of the time becomes O (n^2). The code is as follows:

classSolution { Public:    intLargestrectanglearea (vector<int> &height) {        intres =0; Stack<int>s; Height.push_back (0);  for(inti =0; I < height.size (); ++i) {if(S.empty () | | height[s.top ()] <Height[i]) s.push (i); Else {                intCur =S.top ();                S.pop (); Res= Max (res, height[cur] * (S.empty ()? I: (I-s.top ()-1))); --i; }        }        returnRes; }};

[Leetcode] Largest Rectangle in histogram histogram with largest rectangle

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.