Monday, November 25, 2019

Container With Most Water Problem

MaxWater

Container With Most Water

An example of the two-pointer method

In [1]:
testdata = [4, 5, 9, 2, 4, 7, 2, 4, 1, 3]
In [2]:
 def maxWater(Height): 
    ptr1 = 0
    ptr2 = len(Height)-1
    max_area = 0
      
    while ptr1 < ptr2: 
        max_area = max(max_area, min(Height[ptr1],Height[ptr2])*(ptr2-ptr1)) 
        if Height[ptr1] < Height[ptr2]: 
            ptr1 += 1
        else: 
            ptr2 -= 1
    return max_area   
In [3]:
print(maxWater(testdata))
28