-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcontainer_with_most_water.py
More file actions
34 lines (28 loc) · 980 Bytes
/
container_with_most_water.py
File metadata and controls
34 lines (28 loc) · 980 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
class Solution: # TLE
def maxArea(self, height: List[int]) -> int:
if len(height) <= 1:
return -1
elif len(height) == 2:
return min(height)
maxArea = 0
for i in range(len(height)):
for j in range(i+1,len(height)):
maxArea = max(maxArea, min(height[i], height[j]) * (j-i) )
return maxArea
# two pointer approach
class Solution: # 72ms
def maxArea(self, height: List[int]) -> int:
if len(height) <= 1:
return -1
elif len(height) == 2:
return min(height)
left = 0
right = len(height) - 1
maxArea = 0
while(left < right):
maxArea = max( maxArea, min(height[left], height[right]) * (right-left))
if height[left] > height[right]:
right -= 1
else:
left += 1
return maxArea