Container with the Most Water

https://leetcode.com/problems/container-with-most-water/

  • Uses 2-pointer technique.
  • We move the shorter side inward each time, while also updating the cur_height
  • Local optimization and trade off
max_area = 0
left, right = 0, len(height) - 1
while left < right:
    # Calculate the area with the current boundary lines
    width = right - left
    cur_left = height[left]
    cur_right = height[right]
    max_left, max_right = cur_left, cur_right
    cur_height = cur_left if cur_left < cur_right else cur_right
    current_area = width * cur_height
    if current_area > max_area:
        max_area = current_area
    # Move the pointer pointing to the shorter line inward
    if height[left] < height[right]:
        while left < right and height[left] <= max_left:
        left += 1
    else:
        while left < right and height[right] <= max_right:
        right -= 1
return max_area