DEV Community

Debesh P.
Debesh P.

Posted on

11. Container With Most Water | LeetCode | Top Interview 150 | Coding Questions

Problem Link

https://leetcode.com/problems/container-with-most-water/


Detailed Step-by-Step Explanation

https://leetcode.com/problems/container-with-most-water/solutions/7449371/most-optimal-solution-beats-100-easy-to-v3sbg


leetcode 11


Solution

class Solution {
    public int maxArea(int[] height) {

        int n = height.length;
        int left = 0;
        int right = n - 1;
        int maxA = 0;

        while (left < right) {
            int area = Math.min(height[left], height[right]) * (right - left);
            maxA = Math.max(maxA, area);

            if (height[left] < height[right]) {
                left++;
            } else {
                right--;
            }
        }

        return maxA;
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)