[Leetcode] [Java] Largest Rectangle in histogram

Source: Internet
Author: User

Title:

Given N non-negative integers representing the histogram ' s bar height where the width of each bar are 1, find the area of Larg EST rectangle in the histogram.


Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3] .


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

Test Instructions:

Given n nonnegative integers to represent the height of the histogram block, each straight square is 1 in width, and the largest rectangle in the histogram is found.

For example (as shown):

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

Algorithm Analysis:

Reference Blog: http://pisxw.com/algorithm/Largest-Rectangle-in-Histogram.html

The problem of the largest area of the rectangle, it is easier to understand the idea is to go from each bar to both sides, with their own height as standard, until the two sides below their own height, and then use their own height multiplied by the width of both sides to get the matrix area. Because we compute the largest matrix of our own target height for any bar, the best results are definitely taken. The complexity of each step is O (n), with a total of n bars, so the time complexity is O (n2).

This side is begging for abarThe top of the left below its heightxPosition and right below the minimum of their heightxPosition, the stack can be used to solve the problem. To solve the left example: if the position height of the top of the stack is higher thanbarThe height of the high, then constantly out of the stack, if the stack is empty, indicatingbarThe left edge of the line can reach the leftmost position, making itleft=-1, otherwise the top position of the stack isbarTo the left edge of thebarInto the stack, in the solution to the nextbarThe left side of the border so that you can iterate over it and know eachbarThe left border position.

The same can be solved bar for each right boundary position. So that each bar can form the largest rectangular area for the height(bar)*(right-left-1) entire time O(n)+O(n)+O(n) , respectively, for the left boundary, to seek the right boundary, the maximum area, so that the total time complexity ofO(n)


AC Code:

<span style= "Font-family:microsoft yahei;font-size:12px;" >public class Solution {public int largestrectanglearea (int[] height) {if (Height==null | | height.length        ==0) return 0;        if (height.length==1) return height[0];        Use the stack to solve the maximum x-coordinate of the left height of each bar less than H (bar), which is recorded as int[] Left=new int[height.length];        Linkedlist<integer> stack=new linkedlist<integer> ();        Stack.push (0); for (int i=0;i

Copyright NOTICE: This article is the original article of Bo Master, reprint annotated source

[Leetcode] [Java] 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.