Explanation
Container With Most Water is a Medium interview problem tagged [Array, Two Pointers]. The winning approach is typically **Two Pointers**.
#11
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 that holds 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
The lines at index 1 and 8 form the max container.
Example 2
Input: height = [1,1]
Output: 1
Only one possible container.
Container With Most Water is a Medium interview problem tagged [Array, Two Pointers]. The winning approach is typically **Two Pointers**.
Each pointer only moves forward/backward once, guaranteeing O(n) after any required sort.
Place two indices and move them based on a comparison or invariant (sorted order, palindrome symmetry, partition).
Curated solution in C#, Python, Java, JavaScript, and C++ — switch languages with the tabs.
Time: O(n log n) or O(n)
Space: O(n)
Before coding, restate the problem, name the pattern (Two Pointers), and write the invariant or DP state in one sentence.
Got it?
Mark #11 Container With Most Water as understood when the pattern clicks.