11. Container With Most Water
Intuition
The key insight is that the area of water contained between two lines is determined by:
- The distance between the lines (width)
- The height of the shorter line (as water can only be contained up to the height of the shorter line)
Approach
- Use two pointers technique, starting from both ends of the array
- Calculate the area between the two pointers
- Move the pointer pointing to the shorter line inward, as this is the only way we might find a larger area
- Keep track of the maximum area found so far
Complexity
- Time complexity: O(n)
- Space complexity: O(1)
Keywords
- Two Pointers