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, such that the container contains the most water.
Return the maximum amount of water a container can store.
Notice that you may not slant the container.
In the brute force approach, we'll iterate over all possible pairs of lines and calculate the area of water that it can contain. This involves a nested loop where the outer loop picks the first line and the inner loop tries all possible second lines. For each pair of lines, we calculate the minimum of these two heights (as water can only be stored up to the shorter line) and multiply it by the distance between them (their indices difference) to get the area. We keep track of the maximum area we've found so far.
H[i] and H[j], taking the smaller one, and the distance between walls (j - i).In this more optimal approach, we use two pointers, one at the beginning and one at the end of the array. In every step, we calculate the area formed by the lines at these two pointers. Then, we move the pointer pointing to the shorter line inward because moving the taller one wouldn't possibly increase the area.