11.Container with most water
Leetcode Blind 75
17th June 2022 ~ Dion Pinto
Description
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. (Problem)
Code (Python)
import math
class Solution:
def maxArea(self, height: List[int]) -> int:
m=-math.inf
l,r = 0,len(height)-1
while(l<r):
area = (r-l)*min(height[l],height[r])
m=max(m,area)
if(height[l] < height[r]):
l+=1
elif(height[l] > height[r]):
r-=1
else:
r-=1
return m
Time Complexity => o(n)
Space Complexity => o(1)