AlgoMaster Logo

Container with Most Water

heights=[1, 8, 6, 2, 5, 4, 8, 3, 7]
1public int maxArea(int[] heights) {
2    int left = 0;
3    int right = heights.length - 1;
4    int maxArea = 0;
5
6    while (left < right) {
7        int width = right - left;
8        int height = Math.min(heights[left], heights[right]);
9        int currentArea = width * height;
10        maxArea = Math.max(maxArea, currentArea);
11
12        if (heights[left] < heights[right]) {
13            left++;
14        } else {
15            right--;
16        }
17    }
18
19    return maxArea;
20}
0 / 27
108162235445863778