LeetCode 11. Container With Most Water

Here is my solution to Container with Most Water in JavaScript.

/**
 * @param {number[]} height
 * @return {number}
 */
var maxArea = function(height) {
    let left = 0;
    let right = height.length-1;
    let maxWater = 0;

    while (left < right) {
        const water = (right-left) * Math.min(height[left], height[right]);

        if (water > maxWater) {
            maxWater = water;
        }

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

    return maxWater;
};

Published on