Container With Most Water

Not SpecifiedLeetcode
View on Platform

🧊 Container With Most Water

Pattern: Two Pointers

Idea: Move the pointer at the smaller height, can move both inwards if height is same


💻 Code


def maxArea(heights: List[int]) -> int:
	ans = 0
	i = 0
	j = len(heights)-1
	while(i<j):

		ans = max(ans,(j-i)*min(heights[i],heights[j]))

		if heights[i]<heights[j]:
			i += 1
		elif heights[i]>heights[j]:
			j -= 1
		else:
			i +=1
			j -= 1
	
	return ans

🔗References

Leetcode

🏢 Companies

Amazon, Google, Microsoft, Facebook, Apple, Adobe, Bloomberg, Goldman Sachs, Swiggy

Local Graph View

Start typing to search
Try: two sum or #Arrays or #Amazon