11. Container With Most Water

 

Maximizing Water Containment with the Two-Pointer Technique in JavaScript

The "Container With Most Water" problem on LeetCode is an intriguing challenge that asks us to find the two lines in a given array that, together with the x-axis, form a container holding the most water. This post will explore an efficient and elegant solution using the two-pointer technique in JavaScript.

Problem Statement

Given an array height of length n, where height[i] is the height of a vertical line at index i, find two lines that, together with the x-axis, form a container that holds the most water.

Example

Input: height = [1,8,6,2,5,4,8,3,7] Output: 49

Intuition

The key to solving this problem efficiently lies in leveraging the fact that the array is sorted. By using two pointers—one starting at the beginning and one at the end of the array—we can systematically find the maximum area by moving the pointers inward based on the heights they point to.

Approach

  1. Initialize Variables:

    • Start with max = 0 to keep track of the maximum area found.

    • Initialize two pointers: si = 0 (start index) and li = height.length - 1 (last index).

  2. Iterate with a While Loop:

    • Use a while loop to continue until the two pointers meet.

    • Calculate the area between the lines at the two pointers using the formula (li - si) * Math.min(height[si], height[li]).

    • Update max if the newly calculated area is greater.

  3. Move Pointers:

    • If the height at the starting pointer (height[si]) is less than the height at the ending pointer (height[li]), increment the starting pointer (si).

    • Otherwise, decrement the ending pointer (li).

  4. Return Result:

    • After the loop, max will hold the maximum area found.

Code Implementation

Here's how you can implement the solution in JavaScript:

javascript
var maxArea = function(height) {
    let max = 0;
    let si = 0;
    let li = height.length - 1;
    while(si < li) {
        max = Math.max((li - si) * Math.min(height[si], height[li]), max);
        if(height[si] < height[li]) {
            si++;
        } else {
            li--;
        }
    }
    return max;
};

Complexity Analysis

  • Time Complexity: O(n), where n is the length of the height array. This is because each element is checked at most once due to the two-pointer technique.

  • Space Complexity: O(1), since we only use a few extra variables regardless of the input size.

Conclusion

The two-pointer technique is a powerful tool for solving problems involving arrays. In this case, it allows us to efficiently find the maximum area that can be contained by two lines in linear time. This approach is both elegant and effective, making it a great example of how algorithmic thinking can lead to optimal solutions.

For more detailed explanations and additional solutions, check out the discussion on LeetCode: .

Happy coding! 😊

Comments

Popular posts from this blog

3Sum Closest: O(n²) Two-Pointer Magic šŸš€ leetcode: 16

Kadane's Algorithm: Maximum Subarray Problem - LeetCode(53) Solution

14. Longest Common Prefix