Container With Most Water
Medium Two Pointers AI round
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
Input: height = [4,8,3,6,2,7]
Output: 28
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
Input: height = [1,5,4,3]
Output: 6
Example 3
Input: height = [6,1,9,10,1,5]
Output: 25
Constraints
2 ≤ height.length ≤ 2 × 10⁵0 ≤ height[i] ≤ 10⁴
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.
Your first AI round is free.
Watch 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