-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcontainer_with_max_water.py
32 lines (26 loc) · 1.02 KB
/
container_with_max_water.py
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
# class Solution:
# def maxArea(self, height: List[int]) -> int:
# #move left pointer or right pointer only when height is smaller
# i, j = 0, len(height) - 1
# capacity = 0
# while i < j:
# #width of container * min_height
# capacity = max(capacity, (j-i) * min(height[i], height[j]))
# if height[i] < height[j]:
# i+=1
# else:
# j-=1
# return capacity
class Solution:
def maxArea(self, height: List[int]) -> int:
left, right = 0, len(height) - 1
maxWaterCapacity = 0
while left <= right:
currentWaterCapacity = (right - left) * min(height[left], height[right])
if maxWaterCapacity < currentWaterCapacity:
maxWaterCapacity = currentWaterCapacity
if height[left] <= height[right]:
left += 1
else:
right -= 1
return maxWaterCapacity