Skip to content

Commit 81ed4d7

Browse files
solved using two pointers
You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]). Find two lines that together with the x-axis form a container, such that the container contains the most water. Return the maximum amount of water a container can store. Notice that you may not slant the container. Example 1: Input: height = [1,8,6,2,5,4,8,3,7] Output: 49 Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
1 parent af80834 commit 81ed4d7

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

container-with-most-water.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution(object):
2+
def maxArea(self, height):
3+
"""
4+
:type height: List[int]
5+
:rtype: int
6+
"""
7+
l=0
8+
r=len(height)-1
9+
max_area=0
10+
while l < r :
11+
area=(r-l)*min(height[r],height[l])
12+
max_area=max(max_area,area)
13+
if height[r] > height[l]:
14+
l=l+1
15+
else:
16+
r=r-1
17+
return max_area
18+

0 commit comments

Comments
 (0)