LeetCode 11: Container With Most Water is a Medium two pointers problem. This Python walkthrough develops a greedy boundary movement solution, ties every code decision to a concrete invariant, and includes the regression check used before publication.
This independent guide paraphrases the task rather than reproducing LeetCode’s prompt. Use the official page for the exact statement, examples, constraints, and submission runner.
| Difficulty | Medium |
|---|---|
| Topic | Two Pointers |
| Reusable pattern | greedy boundary movement |
| Complexity | O(n) time and O(1) extra space |
Recognizing the pattern
Two pointers are useful when one comparison lets you permanently discard one side of the remaining search range.
For this problem specifically, measure the area between the endpoints, then move the shorter wall because keeping it cannot improve area at a smaller width. The invariant worth writing beside the code is: The best area using every discarded shorter boundary has already been considered.
Step-by-step algorithm
- Identify the input state consumed by
maxArea(height)and initialize the data required by the greedy boundary movement pattern. - Measure the area between the endpoints, then move the shorter wall because keeping it cannot improve area at a smaller width.
- After each update, verify the page’s central invariant: The best area using every discarded shorter boundary has already been considered.
- Finish only after the boundary behavior is covered: Equal walls may move either side; width decreases each iteration.
Python solution
class Solution:
def maxArea(self, height):
left, right, best = 0, len(height) - 1, 0
while left < right:
best = max(best, (right - left) * min(height[left], height[right]))
if height[left] <= height[right]: left += 1
else: right -= 1
return bestReading the implementation
The main entry point is maxArea(height). The named working state includes left, best; those variables make the greedy boundary movement state visible instead of hiding it in incidental control flow.
A single main loop advances the algorithm, which is the key reason the traversal does not revisit already resolved input unnecessarily. In concrete terms, measure the area between the endpoints, then move the shorter wall because keeping it cannot improve area at a smaller width.
Correctness argument
Initialization. Before the scan begins, the unresolved range contains every candidate and the processed range is empty, so no answer has been lost.
Preservation. Measure the area between the endpoints, then move the shorter wall because keeping it cannot improve area at a smaller width. The chosen movement discards only candidates that cannot improve the answer; afterward, the best area using every discarded shorter boundary has already been considered.
Termination. A boundary advances on every iteration. Once the active range is exhausted, every viable candidate was either measured or safely eliminated, so the stored result is optimal.
Complexity and trade-offs
O(n) time and O(1) extra space. The auxiliary-space figure excludes the returned output unless the output itself is the structure being built.
A quadratic pair scan is easier to invent but repeats comparisons that pointer movement can eliminate. That comparison is useful in an interview because it explains why the final implementation is preferable, not merely that it passes.
Regression check
The published implementation belongs to a 100-problem suite that is compiled and exercised behaviorally before deployment.
One reference assertion from that suite is shown below. It targets the normal path while the edge conditions in the next section cover the failure-prone boundaries.
assert Solution().maxArea([1,8,6,2,5,4,8,3,7]) == 49Common mistakes and edge cases
- Problem-specific boundary: Equal walls may move either side; width decreases each iteration.
- Pattern-level pitfall: Move the pointer justified by the comparison, and state whether the active interval includes or excludes each endpoint.
- Invariant check: after every update, confirm that the best area using every discarded shorter boundary has already been considered.
Interview review checklist
- Explain why greedy boundary movement matches the structure of this input.
- State the invariant in one sentence before tracing code: The best area using every discarded shorter boundary has already been considered.
- Derive O(n) time and O(1) extra space from how many times each element or state is visited.
- Test the boundary explicitly: Equal walls may move either side; width decreases each iteration.
Browse the searchable 100 LeetCode Python Solutions hub. Previous: 167. Two Sum II – Input Array Is Sorted · Next: 15. 3Sum