Leetcode largest Rectangle in histogram (Python detailed and implementation) __python

Source: Internet
Author: User

Topic

Given n non-negative integers representingthe histogram ' s bar height where the width of each bar are 1, find the area Oflar Gest rectangle in the histogram.

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

The largest rectangle is shown in theshaded area and which has area = ten unit.

For example,

Given Heights = [2,1,5,6,2,3],

Return 10.

Ideas

Use stacks to simulate, traverse heights arrays, and compare the size of stack[-1 with stack top elements:

L larger than the top of the stack, push it in;

L equals the top element of the stack, ignore the

L is smaller than the top of the stack, continues to stack, and records the maximum area of this height until the stack is empty. Then replace all the pop-up numbers with the descending point value, which means that the whole is still ordered and not dropped.

Throughout the process, all the local maximum matrices have been computed, and all the scenes have been preserved in the width range.

width = Current index-Pre index

Example, the 2,1,5,6,3 simulation process.

First add a 0, easy to finally all stack out. Become: 2,1,5,6,2,3.

U 2 into stack, stack = [2],maxarea = 0;

U 1 is smaller than stack top element stack[-1], 2 out of stack, Maxarea = 2*1 =2;2 is replaced with 1 into stack, 1 continues into stack, stack = [1,1], Maxarea = 2;

U 5 is larger than stack top element stack[-1], 5 is in stack, stack is stack = [1,1,5], Maxarea = 2;

U 6 is larger than stack top element stack[-1], 6 is in stack, stack is stack = [1,1,5,6], Maxarea = 2;

U 2 smaller than stack top element stack[-1] is a descending point, at which point the stack length is 4, start the stack, 6 out of the stack, Maxarea =6*1=6 (the current height*1 (number 6 in the stack subscript)), and then judged that 2 than the top of the stack element 5 small, 5 out of Stack Maxarea =5*2= 6 (the current height = 5,width =4-2=2 (the subscript number 5 in the stack)); the next 1, 2 is small, no stack is required. Then will eject 5, 6 of the empty pressure stack for 2,2 continue into the stack, stack = [1,1,2,2,2],maxarea = 10;

U 3 is larger than stack top element 2, into stack, stack = [1,1,2,2,2,3],maxarea = 10;

U finally judge the maximum area of each point as the starting point, Max (height[i]* (size-i)) =max{3*1, 2*2, 2*3, 2*4, 1*5, 1*6}= 8 < 10. Traversal ended. Max's area is 10 throughout the process.

"Python Implementation"

Method One: Brute force algorithm, all traversal (TLE)

Class Solution:

"": Type Heights:list[int]

: Rtype:int

"""

def largestrectanglearea (self, height):

Maxarea=0

For I in range (len (height)):

min = Height[i]

For j in range (I, Len (height)):

If Height[j] < min:

min = Height[j]

If min* (j-i+1) > Maxarea:

Maxarea = min* (j-i+1)

Return Maxarea

if __name__ = = ' __main__ ':

s= Solution ()

Heights = [2,1,5,6,2,3]

Maxarea = S.largestrectanglearea (heights)

Print (Maxarea)

Method Two: Stack simulation

Class Solution:

"": Type Heights:list[int]

: Rtype:int

"""

def largestrectanglearea (self, Heights):

Maxarea = 0

stack = []

i = 0

While I < Len (heights):

If Len (stack) = = 0 or stack[-1] <= Heights[i]:

Stack.append (Heights[i])

Else

Count = 0

While Len (stack) > 0 andstack[-1] > heights[i]: #height [i] is less than the top element of the stack

Count + 1

Maxarea = max (Maxarea,stack[-1]*count)

Stack.pop () #栈顶元素出栈

While Count > 0: #将当前height入栈

Count = 1

Stack.append (Heights[i])

Stack.append (Heights[i])

i + 1

Count = 1

While Len (stack)!= 0:

Maxarea = Max (Maxarea, Stack[-1]*count)

Stack.pop ()

Count + 1

Return Maxarea

if __name__ = = ' __main__ ':

s= Solution ()

Heights = [2,1,5,6,2,3]

Maxarea = S.largestrectanglearea (heights)

Print (Maxarea)

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.