I use two pointers at the edges and calculate the area formed by the two heights. Since the container height is limited by the shorter line, I move the pointer pointing to the shorter line inward to try and maximize the area. I repeat until the two pointers meet, keeping track of the maximum area found.
class Solution:
def maxArea(self, height: List[int]) -> int:
left, right = 0, len(height) - 1
max_area = 0
while left < right:
width = right - left
h = min(height[left], height[right])
max_area = max(max_area, width * h)
if height[left] < height[right]:
left += 1
else:
right -= 1
return max_area- Time: O(n)
- Space: O(1)
Dope