-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.py
51 lines (35 loc) · 1.13 KB
/
11.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'''11. Container With Most Water
Created on 2024-12-02 23:09:05
2024-12-02 23:50:34
@author: MilkTea_shih
'''
#%% Packages
#%% Variable
#%% Functions
#%% Main Function
class Solution:
def maxArea(self, height: list[int]) -> int:
result: int = 0
left, right = 0, len(height) - 1 #two-pointer
# The biggest answer is more likely to appear
# where the two lines are furthest apart.
# Therefore, there may be redundant calculations
# when `left` and `right` are almost close to the middle.
height_maximum: int = max(height)
while left < right:
result = max(
#formula for the amount of water that a container can store
min(height[left], height[right]) * (right - left),
result
)
if height[left] < height[right]:
left += 1
else:
right -= 1
if height_maximum * (right - left) < result:
break # Early break when `result` is never bigger.
return result
#%% Main
if __name__ == '__main__':
pass
#%%