-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path0011_Container_With_Most_Water.py
More file actions
36 lines (29 loc) · 1015 Bytes
/
0011_Container_With_Most_Water.py
File metadata and controls
36 lines (29 loc) · 1015 Bytes
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
26
27
28
29
30
31
32
33
34
35
36
'''
# Approach
1. Start with a pointer on each end
1. `l = 0`
2. `r = len(height) - 1`
2. Calculate the area (base x height) which can hold water
1. base = distance between `l` and `r`
2. height = the lesser of `l` and `r`'s values in `height`
3. Update `max_area` if the calculated area is greater
4. Using their indexed values in `height`, move `l` and `r` together based on which is shorter
5. Repeat steps 2 to 4 until `l` and `r` meet somewhere in the middle
6. Return the `max_area`
https://leetcode.com/problems/container-with-most-water/description/
'''
class Solution:
def maxArea(self, height: List[int]) -> int:
max_area = 0
l = 0
r = len(height) - 1
while l < r:
base = r - l
h = min(height[l], height[r])
if base * h > max_area:
max_area = base * h
if height[l] < height[r]:
l += 1
else:
r -= 1
return max_area