philosyang.com

11. Container With Most Water: Two Pointers

a good refresher on the basics of two-pointers.

Two pointers (O(1) space) example solution:

 1class Solution:
 2    def maxArea(self, height: List[int]) -> int:
 3        result = 0
 4        l = 0
 5        r = len(height) - 1
 6
 7        while l < r:
 8            result = max(result, min(height[l], height[r]) * (r - l))
 9            if height[l] < height[r]:
10                l += 1
11            else:
12                r -= 1
13
14        return result

#Neetcode150 #Two-Pointers #Python