Container With Most Water
You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).
Find two lines that together with the x-axis form a container, that can hold the most water. Return the maximum amount of water the container can store.
The container sides must be vertical; you may not slant the container.
Example 1
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 1 8 6 2 5 4 8 3 7
Input
height = [1,8,6,2,5,4,8,3,7]Output
49The lines at indices 1 and 8 have heights 8 and 7, giving an area of min(8, 7) * (8 - 1) = 49.
Example 2
# # 1 1
Input
height = [1,1]Output
1The only possible container uses both lines and holds min(1, 1) * 1 = 1 unit of water.
Constraints
- 2 <= height.length <= 10^5
- 0 <= height[i] <= 10^4