-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwater_container.py
More file actions
39 lines (30 loc) · 961 Bytes
/
water_container.py
File metadata and controls
39 lines (30 loc) · 961 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
37
38
39
class Solution(object):
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""
# largest area
largest_area = 0
# current area
current_area = 0
# right pointer
i = 0
# left pointer
j = len(height) - 1
while i < j:
# calculate the area
# lowest of the two heights times width
low_height = min(height[i], height[j])
current_area = low_height * (j-i)
# if the new area is larger than previous largest
largest_area = max(largest_area, current_area)
if height[i] <= height[j]:
i += 1
else:
j -= 1
# return largest stored area
return largest_area
height = [1, 8, 6, 2, 5, 4, 8, 3, 7]
result = maxArea(height)
print(result)