Medium
Container With Most Water
- Time limit
- 2 s
- Memory limit
- 256 MB
- Estimated
- 20 min
- Two Pointers
Description
Description
You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the line i are at coordinates (i, 0) and (i, height[i]).
Choose two lines, which together with the x-axis form a container, such that the container holds the most water.
The container cannot be slanted: the amount of water held is determined by the shorter of the two chosen lines and the horizontal distance between them. Return the maximum amount of water the container can store.
Examples
Example 1
- Input:
height = [1, 8, 6, 2, 5, 4, 8, 3, 7] - Output:
49 - Explanation: Choose lines at indices
1and8. The width is7and the height ismin(8, 7) = 7, giving7 × 7 = 49.
Example 2
- Input:
height = [1, 1] - Output:
1
Constraints
2 <= height.length <= 1000000 <= height[i] <= 10000- The answer fits in a signed 32-bit integer.
Sample cases
3Sample 1
Input
height = [1,8,6,2,5,4,8,3,7]
Expected output
49
Sample 2
Input
height = [1,1]
Expected output
1
Skills and languages
Patterns
- Two Pointers
Languages
- Python 3
Solve this problem
Create a free account to write, run and submit your solution.
- Write Python or Java in the browser, with your draft saved as you type
- Run your code on the examples as often as you like
- Submit to a sandboxed judge and see exactly which case failed
- Ask the AI mentor for a hint without being handed the answer