[Leetcode] Largest Rectangle in histogram

Source: Internet
Author: User

Title: (Stack)

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 .

Exercises

Reference http://www.cnblogs.com/lichen782/p/leetcode_Largest_Rectangle_in_Histogram.html

Difficulties, thinking more difficult to think. Look more.

 Public classSolution { Public intLargestrectanglearea (int[] height) {Stack<Integer> stack =NewStack<integer>(); int[] h =New int[height.length+1]; intI=0; intresult=0; H= arrays.copyof (height, height.length+1);  while(i<h.length) {if(Stack.isempty () | | H[i]>H[stack.peek ()]) Stack.push (i++); Else            {                intt =Stack.pop (); Result= Math.max (result, h[t]* (Stack.isempty ()? I:i-stack.peek ()-1)); }        }        returnresult; }}

[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.