DEV Community

Cover image for Array- Q3 Container With Most Water
Pranjal Sailwal
Pranjal Sailwal

Posted on

Array- Q3 Container With Most Water

class Solution {
    public int maxArea(int[] height) {
        int left = 0;
        int right = height.length - 1;
        int maxArea = 0;
        while (left < right) {
            int currentHeight = Math.min(height[left], height[right]);
            int currentWidth = right - left;
            int currentArea = currentHeight * currentWidth;
            maxArea = Math.max(maxArea, currentArea);
            if (height[left] < height[right]) {
                left++;
            } else {
                right--;
            }
        }
        return maxArea;
    }
}
Enter fullscreen mode Exit fullscreen mode

Open to updates and suggestions.

Top comments (0)