Categories
interview

Container With Most Water

/**
 * @param {number[]} height
 * @return {number}
 */
const maxArea = (height) => {
  let low = 0;
  let high = height.length - 1;
  let max = 0;
  while (low < high) {
    max = Math.max(max, (high - low) * Math.min(height[low], height[high]));
    if (height[low] <= height[high]) {
      low++
    } else {
      high--;
    }
  }
  return max;
};

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

Demo