
Leetcode Problem 11: Container With Most Water
You are given an array height of n integers. Consider n vertical lines where line i has height height[i]. Find two lines that, together with the x-axis, form a container holding the most water. Return the maximum water volume. The water level is limited by the shorter line, and the volume equals the shorter height multiplied by the horizontal distance between the lines.
Constraints:
function maxArea(height: number[]): number {
let first = 0
let last = height.length - 1
let maxArea = 0
while (first < last) {
let smallerSide = height[first] > height[last] ? height[last] : height[first]
maxArea = Math.max(maxArea, smallerSide * Math.abs(last - first))
if (height[first] > height[last]) {
last--
} else {
first++
}
}
return maxArea
};
console.log(maxArea([1,8,6,2,5,4,8,3,7]))