Given an array of height n in this problem. We have a bar graph that has n verticle lines of a given array and has endpoints of i will be height[i].
We have to find the area between the 2 lines on the x-axis such they create a container that fills the most water.
You have to return the maximum amount of water a container can store.
Note You cannot lean the container to maximize the area of container.
Example 1:
Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49
Explanation: In this bar graph, we find the maximum area between two verticle lines is 49 (represented by blue area).Given array is height=[1,8,6,2,5,4,8,3,7].
Example 2:
Input: height = [1,1]
Output: 1
Constraints:
n == height.length
2 <= n <= 105
0 <= height[i] <= 104
Effective solution:
Approach: Given a range of container boundary line heights, find the maximum amount of water that can be stored in the container. So start with the first and last element and check the amount of water it can contain and store that container. Now the question arises whether there are any better boundaries or lines which can contain the maximum of water. So there is a smart way to find out. Initially, there are two indices, the first and last index pointing to the first and last element (which acts as the container boundary), if the value of the first index is less than the value of the last index increment the first index, otherwise decrement the last index. Since the loss of width is constant, following the above procedure can lead to an optimal response.