Container With Most Water
Medium Two Pointers
height is a row of upright lines standing on flat ground, one unit apart: the line at position i is height[i] units tall.
Any two of the lines, with the ground between them, make a container. It holds water up to the shorter of the two lines, so the water it holds is that shorter height times the distance between the two lines.
Return the most water that a container made of two of the lines can hold.
Example 1
The lines at positions 1 and 5 are 8 and 7 tall and 4 apart. The water rises to 7, the shorter of them, so they hold 7 × 4 = 28. No other two lines hold more.
Example 2
Example 3
Constraints
2 ≤ height.length ≤ 2 × 10⁵0 ≤ height[i] ≤ 10⁴
Solve it here
Write it in Python, Java or C++, run it on your own cases, and submit against hidden tests.
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
See the AI roundWatch the explanation
More Two Pointers
- Trapping Rain WaterAI round · Hard
- 3SumAI round · Medium
- Longest Palindromic SubstringAI round · Medium
- Find the Duplicate NumberAI round · Medium
- Sort ColorsAI round · Medium