Pointers Pointers

Max Water

~2 mins read

1
2
max water among sticks:
    move the shorter line
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
"""
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.
"""
def maxArea(self, height: List[int]) -> int:
    def area(l,r):
        h = min(height[l],height[r])
        w = r-l
        return h*w

    l = 0
    r = len(height)-1
    max_area = 0 

    while l<r:
        max_area = max(max_area, area(l,r))
        # move the shorter line 
        if height[l]<height[r]:
            l += 1
        else:
            r -= 1

    return max_area 

🎰