hardTwo PointersPattern: Two Pointers
Container Water Volume Solution
Problem Statement
Given a list of non-negative integers `heights` representing the heights of a series of containers, calculate the total volume of water that can be trapped between them.
Constraints
- 1 <= n <= 2 * 10^4
- 0 <= height[i] <= 10^5
Time: O(N) Space: O(1)
Two pointers (left, right) and two variables (leftMax, rightMax). If leftMax < rightMax, we know the water level at 'left' is determined by leftMax. Add (leftMax - height[left]) to total, increment left. Else do the same for right. Time O(N), Space O(1).
Run, Test & Submit Code
Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.
Solve on Interactive WorkspaceTested Solutions
No solution code is currently loaded.
Complete this code in the workspace editor.
