[Leetcode] [Python] Container with most water

Source: Internet
Author: User

#-*-Coding:utf8-*-
‘‘‘
https://oj.leetcode.com/problems/container-with-most-water/

Given n non-negative integers a1, A2, ..., an, where each represents a point at coordinate (I, AI).
n vertical lines is drawn such, the and the other 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.

===comments by dabay===
The problem is classic, but I still don't know why I can. Just remember the solution, fennel beans will be a kind of writing.

Two pointers, One direction from left to right and the other from right to left.
Calculates the maximum capacity and updates the Max_area.
Then move, decimal pointer!! (That is, if the number of left pointers is smaller than the number of right pointers, the left pointer moves to the right, to the next calculation; On the other hand, the right pointer moves to the left, and the next calculation is entered)
Exit when encountered.
‘‘‘
Class Solution:
# @return An integer
def maxarea (self, height):
Max_area = 0
i = 0
j = Len (height)-1
While I < j:
s = min (Height[i], height[j]) * (j-i)
Max_area = Max (S, Max_area)
If height[i] < Height[j]:
i = i + 1
Else
j = J-1
Return Max_area


def main ():
s = solution ()
Print S.maxarea ([1,2,3,1,3,1])


if __name__ = = "__main__":
Import time
Start = Time.clock ()
Main ()
Print "%s sec"% (Time.clock ()-start)

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