containerWithMostWater.md
May 25, 2025 ยท View on GitHub
Description:
Given an integer array heights of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, heights[i]). You need to find two lines that together with the x-axis form a container, such that the container contains the most water. Return the maximum amount of water a container can store.
Examples
Example 1:
Input: heights = [3, 9, 4, 1, 5, 4, 7, 1, 7] Output: 49
Example 2:
Input: heights = [1, 1] Output: 1
Example 3 (Edge Case):
Input: heights = [5] Output: 0 (Need at least two lines to form a container)
Algorithm overview: This problem is solved using the two pointers technique, starting from opposite ends of the array. The key insight is that the area of water is determined by the shorter of the two lines and the distance between them.
Detailed steps:
-
Initialize Variables
- Set
maxAreato 0 to track the maximum water container capacity - Set
leftpointer to the beginning (index 0) of the array - Set
rightpointer to the end (index length-1) of the array
- Set
-
Two-Pointer Traversal
Continue while theleftpointer is less than therightpointer. -
Calculate Current Area
- Find the height of the container as the minimum of the two heights at current pointers:
height = Math.min(heights[left], heights[right]) - Calculate the width as the distance between pointers:
width = right - left - Compute the area:
area = height * width
- Find the height of the container as the minimum of the two heights at current pointers:
-
Update Maximum Area
UpdatemaxAreaif the current area is larger:maxArea = Math.max(maxArea, area) -
Move Pointers Strategically
- If the left height is shorter than the right height, increment the
leftpointer - Otherwise, decrement the
rightpointer
Note: We always move the pointer pointing to the shorter line because:
- The container's height is limited by the shorter line
- Moving the pointer at the shorter line gives a chance to find a taller line that might increase the area
- Moving the pointer at the taller line would only decrease the width without the possibility of increasing the height
- If the left height is shorter than the right height, increment the
-
Return Result
After the pointers meet, return themaxAreaas the maximum water container capacity.
Time and Space Complexity
-
Time Complexity:
O(n)
The algorithm makes a single pass through the array with the two pointers, examining each element at most once. -
Space Complexity:
O(1)
The algorithm uses only a constant amount of extra space regardless of input size, as it only requires a few variables (maxArea,left,right, etc.) to track the state.