31. Container With Most Water
β GFG solution to the Container With Most Water problem: find maximum water area between two vertical lines using optimal two-pointer technique. π
The problem can be found at the following link: π Question Link
π§© Problem Description
Given an array arr[]
of non-negative integers, where each element arr[i]
represents the height of vertical lines, find the maximum amount of water that can be contained between any two lines, together with the x-axis.
Note: In the case of a single vertical line, it will not be able to hold water.
The water area is calculated as: Area = min(height[i], height[j]) Γ (j - i) where i and j are indices of two lines.
π Examples
Example 1
Input: arr[] = [1, 5, 4, 3]
Output: 6
Explanation: Lines at indices 1 and 3 with heights 5 and 3 are 2 distance apart.
Base = 2, Height = min(5, 3) = 3. Total area = 3 Γ 2 = 6.
Example 2
Input: arr[] = [3, 1, 2, 4, 5]
Output: 12
Explanation: Lines at indices 0 and 4 with heights 3 and 5 are 4 distance apart.
Base = 4, Height = min(3, 5) = 3. Total area = 3 Γ 4 = 12.
Example 3
Input: arr[] = [2, 1, 8, 6, 4, 6, 5, 5]
Output: 25
Explanation: Lines at indices 2 and 7 with heights 8 and 5 are 5 distance apart.
Base = 5, Height = min(8, 5) = 5. Total area = 5 Γ 5 = 25.
π Constraints
$1 \le \text{arr.size()} \le 10^5$
$0 \le \text{arr}[i] \le 10^4$
β
My Approach
The optimal approach uses the Two Pointers technique with a Greedy Strategy to efficiently find the maximum water area:
Two Pointers + Greedy Algorithm
Initialize Pointers:
Use two pointers:
left
at start (0) andright
at end (n-1).Initialize
maxArea
to track the maximum water area found.
Calculate Current Area:
For current pointers, calculate area =
min(h[left], h[right]) Γ (right - left)
.Update
maxArea
if current area is larger.
Greedy Pointer Movement:
Move the pointer with the smaller height inward.
Why? Moving the pointer with larger height would only decrease the width while keeping the limiting height same or smaller, never improving the area.
Convergence:
Continue until
left >= right
.The algorithm guarantees we won't miss the optimal solution due to the greedy property.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), where n is the size of the array. Each element is visited at most once as we use two pointers moving towards each other.
Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space for variables like pointers and maxArea.
π§βπ» Code (C++)
class Solution {
public:
int maxWater(vector<int>& h) {
int l = 0, r = h.size() - 1, ans = 0;
while (l < r) {
ans = max(ans, min(h[l], h[r]) * (r - l));
h[l] < h[r] ? ++l : --r;
}
return ans;
}
};
β Code (Java)
class Solution {
public int maxWater(int[] h) {
int l = 0, r = h.length - 1, max = 0;
while (l < r) {
max = Math.max(max, Math.min(h[l], h[r]) * (r - l));
if (h[l] < h[r]) l++; else r--;
}
return max;
}
}
π Code (Python)
class Solution:
def maxWater(self, h):
l, r, ans = 0, len(h) - 1, 0
while l < r:
ans = max(ans, min(h[l], h[r]) * (r - l))
l, r = (l + 1, r) if h[l] < h[r] else (l, r - 1)
return ans
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated