LeetMotion
Given an integer array height of length n, where height[i] represents a vertical line at position i, find two lines that together with the x-axis form a container that holds the most water.
Return the maximum amount of water a container can store.
Try every combination of two lines and compute the water they can hold. Track the maximum area found.
(i, j) with i < j, compute area = min(height[i], height[j]) × (j - i).maxArea if this area is larger.maxArea.Why it's not optimal: O(n²) time — too slow for n up to 10⁵. The two-pointer approach eliminates impossible pairs greedily and runs in O(n).
def maxArea(height): maxA = 0 for i in range(len(height)): for j in range(i + 1, len(height)): area = min(height[i], height[j]) * (j - i) maxA = max(maxA, area) return maxA
The area between two lines is limited by the shorter one. If we move the taller wall inward, we can only decrease or maintain width — with no chance of increasing height. But if we move the shorter wall, we might find a taller wall and gain area.
L=0 and R=n-1 (widest container).min(height[L], height[R]) × (R-L), update max.
Discussion
…