-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
28 lines (23 loc) · 905 Bytes
/
main.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
class Solution:
def maxArea(self, height: list[int]) -> int:
# Initialize two pointers at the ends of the list
left, right = 0, len(height) - 1
max_area = 0
# Iterate until the two pointers meet
while left < right:
# Calculate the area
width = right - left
current_height = min(height[left], height[right])
current_area = width * current_height
# Update the maximum area
max_area = max(max_area, current_area)
# Move the pointer of the shorter line inward
if height[left] < height[right]:
left += 1
else:
right -= 1
return max_area
# Example usage:
sol = Solution()
print(sol.maxArea([1, 8, 6, 2, 5, 4, 8, 3, 7])) # Output: 49
print(sol.maxArea([1, 1])) # Output: 1