DSA · Arrays & Two Pointers · #56
Container With Most Water
Module 40 · difficulty 3/5·⏱ 30:00starts on first keystroke
You are given an integer array `height` of length `n`. Each element `height[i]` represents a vertical line drawn at x-coordinate `i` with that height. Two lines, together with the x-axis, form a container that holds water. Implement `maxArea(height)` that returns the **maximum amount of water** a container can store. The area between lines `i` and `j` (with `i < j`) is `min(height[i], height[j]) * (j - i)`. You may not slant the container.
Examples
height = [1,8,6,2,5,4,8,3,7]→49— Lines at indices 1 and 8 (heights 8 and 7) give min(8,7) * (8-1) = 7 * 7 = 49.height = [1,1]→1— Only one container possible: min(1,1) * 1 = 1.height = [4,3,2,1,4]→16— Indices 0 and 4 (both height 4): min(4,4) * 4 = 16.
Constraints
- · n == height.length
- · 2 <= n <= 10^5
- · 0 <= height[i] <= 10^4
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.