#-*-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