[Leetcode] Largest Rectangle in Histgram, problem solving report

Source: Internet
Author: User

Catalogue

    • Directory
    • Topic
    • Idea One
    • Two ideas

Topics

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 = ten unit.

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

Idea one

We use the simplest two-tier for loop method. For each column row graph I's height H, we extend the width of I to the left and right respectively, to find out the maximum rectangular area after each point of the graph, and then take the maximum value.

The limitations of the left and right extension are:

0 && height[i] >= hi < height.length && height[i] >= h

The code is as follows:

 Public  class solution {     Public int Largestrectanglearea(int[] height) {if(Height = =NULL|| Height.length = =0)return 0;intArea =0; for(inti =0; i < height.length; i++) {intHigh = Height[i];intwidth =1; for(intj = i-1; J >=0&& Height[j] >= high;            j--) {width++; } for(intj = i +1; J < height.length && Height[j] >= high;            J + +) {width++;        } area = Math.max (area, high * width); }returnArea }}

One problem, however, is that this approach cannot be tested on Leetcode by large sets because the time complexity of the algorithm is O (n^2). Next, let's look at how the time complexity of O (n^2) is reduced to O (n).

Idea two

This paper presents an O (n) solution using stacks. In order to reduce the complexity of time, it is necessary to increase the complexity of space accordingly.

We use stacks to store the height increment of the index array. Because, for each column chart height, there are two kinds of situations:

    1. When the stack is empty or the current height is greater than the height of the top subscript of the stack, the current subscript is in the stack.
    2. In the case of not 1, the stack is marked at the top of the current stack, and the maximum rectangle representing the height of the current stack top subscript is computed. Since the height has already been determined, the width is divided into two cases (assuming the subscript of the Traverse Point is i):

      • When the stack is empty, the width is the size of the current traverse point I. (The left boundary is 0 because the stack cannot be empty if there is a higher point on the left than the current subscript height.) The right boundary is i-1, because the height of the I subscript is less than the current subscript height of the stack. So the final width is: [i-1-0 + 1] = [i]).
      • When the stack is not empty, the width is I-stack.peek ()-1. (The left boundary is subscript + 1 for Stack.peek () because the height of the current stack.peek () is less than H. The right boundary is the current coordinate i-1. So the final width is: [I-1-(Stack.peek () + 1) + 1] = [I-stack.peek ()-1]).

AC Code

 Public  class solution {     Public int Largestrectanglearea(int[] height) {if(Height = =NULL|| Height.length = =0)return 0;intArea =0, Len = height.length; linkedlist<integer> stack =NewLinkedlist<integer> (); for(inti =0; i < Len; i + +) {if(Stack.isempty () | | height[stack.peek ()] <= height[i])            {Stack.push (i); }Else{intindex = Stack.pop ();intHigh = Height[index];intwidth = Stack.isempty ()? I:i-Stack.peek ()-1;                Area = Math.max (area, high * width); I-=1; }        } while(!stack.isempty ()) {intindex = Stack.pop ();intHigh = Height[index];intwidth = Stack.isempty ()? Len:len-stack.peek ()-1;        Area = Math.max (area, high * width); }returnArea }}

[Leetcode] Largest Rectangle in Histgram, problem solving report

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.