37 lines
954 B
Python
37 lines
954 B
Python
def maxArea(height):
|
|
"""
|
|
:type height: List[int]
|
|
:rtype: int
|
|
"""
|
|
# n = len(height)
|
|
# container = []
|
|
|
|
# # Handle edge-case
|
|
# if n == 2:
|
|
# return min(height)
|
|
|
|
# for i in range(n):
|
|
# for j in range(i+1,n):
|
|
# index = min(height[i], height[j])
|
|
# dist = j-i
|
|
# container.append(index*dist)
|
|
# return max(container)
|
|
# # return container
|
|
|
|
# Solution O(N) https://youtu.be/Kb20p6zy_14?si=6B86Iwh0-O_Cww0Q
|
|
max_area = 0
|
|
l = 0
|
|
r = len(height) - 1
|
|
while l < r:
|
|
area = (r - l) * min(height[r], height[l])
|
|
max_area = max(max_area, area)
|
|
if height[l] < height[r]: # Check to see if left or right is smaller
|
|
l += 1
|
|
else:
|
|
r -= 1
|
|
return max_area
|
|
|
|
print(maxArea([1,8,6,2,5,4,8,3,7])) #Output: 49
|
|
print(maxArea([1,2,1])) #Output: 2
|
|
print(maxArea([1,2])) #Output: 1
|
|
print(maxArea([1,2,4,3])) #Output: 4 |