DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

Container With Most Water

Container With Most Water

/**
 * @param {number[]} height
 * @return {number}
 */
var maxArea = function (height) {
  let capacity = -Infinity;

  let start = 0;
  let end = height.length - 1;

  while (start <= end) {
    let W = end - start;
    let H = Math.min(height[start], height[end]);
    capacity = Math.max(capacity, W * H);

    if (height[start] > height[end]) {
      end--;
    } else {
      start++;
    }
  }

  return capacity;
};

console.log(maxArea([1, 8, 6, 2, 5, 4, 8, 3, 7]));

Enter fullscreen mode Exit fullscreen mode

Top comments (0)