#11
Container With Most Water
medium· Two Pointersruns: 0You are given an integer array heights of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, heights[i]). Find two lines that together with the x-axis form a container that holds the most water. Return the maximum amount of water the container can store.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 421
class Solution:
def maxArea(self, heights: list[int]) -> int:
left, right = 0, len(heights) - 1
best = 0
while left < right:
width = right - left
area = width * min(heights[left], heights[right])
best = max(best, area)
if heights[left] < heights[right]:
left += 1
else:
right -= 1
return best
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.