Skip to content

Commit 322bffd

Browse files
committed
container-with-most-water
1 parent 4938349 commit 322bffd

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
Time complexity O(n)
3+
Space complexity O(1)
4+
5+
Two pointer
6+
"""
7+
8+
class Solution:
9+
def maxArea(self, height: List[int]) -> int:
10+
s, e = 0, len(height) - 1
11+
area = 0 # max area
12+
while s < e:
13+
h = min(height[s], height[e])
14+
tmp = h * (e - s) # area at current iteration
15+
area = max(area, tmp)
16+
# move pointer
17+
if height[s] < height[e]:
18+
s += 1
19+
else:
20+
e -= 1
21+
22+
return area

0 commit comments

Comments
 (0)