Container with Most Water
Link:
Type: Array
Difficulty: Medium
How to think of the problem
Two pointer solution.
You're always tracking the left and right pointers to figure out which is the minimum, then multiplying by the area
Solution
class Solution(object):
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""
#iterate through the array
#left and right pointers, l=0, r=len(height)- 1
#max = left * right, then max=max(left*right)
#move the smaller of the difference between them ie l=8, l-l+1 = 1, r=7 r-(r-1) = 3
#calculate max
l,r = 0, len(height)-1
maxWater = 0
while l<r:
minheight = min(height[l],height[r])
maxWater = max(maxWater,(minheight*(r-l)))
if height[l] < height[r]:
l += 1
else:
r -=1
return maxWater