Skip to content

Commit 865898b

Browse files
committed
- Container With Most Water #242
1 parent 3ca8168 commit 865898b

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from typing import List
2+
3+
class Solution:
4+
"""
5+
- Time Complexity: O(n), n = len(height)
6+
- Space Complexity: O(1)
7+
"""
8+
def maxArea(self, height: List[int]) -> int:
9+
l, r = 0, len(height) - 1
10+
max_area = float("-inf")
11+
12+
while l < r:
13+
area = (r - l) * min(height[l], height[r])
14+
max_area = max(max_area, area)
15+
if height[l] < height[r]:
16+
l += 1
17+
else:
18+
r -= 1
19+
20+
return max_area
21+
22+
tc = [
23+
([1,8,6,2,5,4,8,3,7], 49),
24+
([1,1], 1),
25+
([0, 1], 0)
26+
]
27+
28+
sol = Solution()
29+
for i, (h, e) in enumerate(tc, 1):
30+
r = sol.maxArea(h)
31+
print(f"TC {i} is Passed!" if r == e else f"TC {i} is Failed! - Expected: {e}, Result: {r}")

0 commit comments

Comments
 (0)