Leetcode: Largest Rectangle in Histogram. java

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 where width of each bar is 1, given height =[2,1,5,6,2,3].


The largest rectangle is shown in the shaded area, which has area =10Unit. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + c1_vcd4kpha + signature + MTA8L2NvZGU + l1_vcd4kpha + signature/Signature + CrXa0rvW1sfpv/fingerprint "" alt = "\">


The circular width from the right is the distance from the subscript to the rightmost, And the length is the height of the current column.

In the second case, if the current one is smaller than the previous one.


Then let the columns higher than this pillar out of the stack, and compare the maximum area of all the bars of the stack.


Public class Solution {public int largestRectangleArea (int [] height) {if (height = null | height. length = 0) return 0; int ans = 0; Stack
 
  
Stack = new Stack
  
   
(); For (int I = 0; I 
 


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.