Container With Most Water in TypeScript

function maxArea(height: number[]): number {
    // Initialize left and right pointers
    let left = 0;
    let right = height.length - 1;
    // Initialize maxArea to store the maximum area found
    let maxArea = 0;
    
    // Continue looping until the left pointer is not the same as the right pointer
    while (left < right) {
        // Calculate the width between the two lines
        const width = right - left;
        // Find the height of the shorter line between the two
        const minHeight = Math.min(height[left], height[right]);
        // Calculate the area with the current configuration
        const area = width * minHeight;
        // Update maxArea if the found area is larger
        maxArea = Math.max(maxArea, area);
        
        // Move the pointer of the shorter line inward
        if (height[left] < height[right]) {
            left++;
        } else {
            right--;
        }
    }
    
    // Return the maximum area found
    return maxArea;
}

PrevNext