Container With Most Water
Last updated
Last updated
Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49
Explanation: The above vertical lines are
represented by array [1,8,6,2,5,4,8,3,7].
In this case, the max area of water
(blue section) the container can contain is
49.class Solution:
def maxArea(self, height: List[int]) -> int:
maxArea = float("-inf")
i = 0
j = len(height) - 1
while i < j:
thisArea = min(height[i], height[j]) * (j-i)
maxArea = max(maxArea, thisArea)
if height[i] < height[j]:
i+=1
else:
j-=1
return maxArea
#Two pointer approach: The two pointer approach can
#be used as we can set both pointers at the end of the
#array and move either pointer forward depending on
#whether ai or aj is the minimum
#Two pointer is the best approach since we need to
#calculate the width between two points