Leetcode algorithm Problem python solution: 11. Container with most water

Source: Internet
Author: User

The 11th question of Leetcode

Given n non-negative integers A1, A2, ..., an, where each represents a point at coord Inate (i, ai). N Vertical Lines is drawn such that the the both endpoints of line I am at (i, ai) and ( i, 0). Find lines, which together with X-axis forms a container, such that the container contains the most water.

Note:you may not slant the container and n are at least 2.

Example:

Input: [1,8,6,2,5,4,8,3,7]output:49

Chinese test instructions is probably the list of random two elements of the index difference as a long, two elements of the small value of the same as high, area = long * high. Take a list of the maximum areas that may appear.

The simplest method is to traverse the two-time list (responsible for the degree O (n^2)), to find out all the area and then take the largest, but here are the algorithm requirements, the complexity is too high will time out, so can only use the lower complexity of the algorithm.

Here's a rule: if you start with a value from both ends, the maximum area that can be generated by the small value has been determined. Because the area of the high take two elements of the smaller one, the largest height has been determined only to select the maximum length, is already the largest index difference at the other end.

For example: a = [2,8,6,1,5,4,8,3,7] a[0] and A[len (a)] compare size, a[0] smaller, then a[0] can produce the maximum value of the element only A[len (a)], because the other elements than a[0] is not meaningful. The higher is the small element, the smaller element than the a[0] is less meaningful, the maximum value needed here. So as long as the element at one end is small, the element that can fetch the largest area must be at the other end. An element with a small value can continue to be taken after it has been taken, because its existence is meaningless.

The code is as follows:

classsolution (object):defMaxarea (self, height): Maxa=0 Front= 0#define two variables as an indexbehind = Len (height)-1#from both ends to the middle value, the small value of the index can be taken directly to the maximum value, take out the index directly eliminated         whileTrue:ifFront = = behind:#take to the same bit of index                 Break            elifHeight[front] >= Height[behind]:#The most front-end number is greater than the last, and the last value is determined, and can be eliminated.Area = height[behind]* (Behind-front) behind-= 1ifArea >Maxa:maxa= AreaElse:#Ibid .Area = height[front]* (Behind-front) Front+ = 1ifArea >Maxa:maxa= AreareturnMaxa

The time complexity of this algorithm is O (n).

The problem-solving ideas refer to other articles in the blog park.

Ry_chen

Leetcode algorithm Problem python solution: 11. Container with most water

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.