The water trapped at any given index is determined by the minimum of the maximum height to its left and the maximum height to its right, minus the current height: Water[i] = \min(\text{maxLeft}_i, \text{maxRight}_i) - \text{height}[i].
Rather than pre-calculating two arrays for left/right maximums (which costs O(N) space), we can use a Two-Pointer approach to calculate the volume in a single pass.
By maintaining leftMax and rightMax variables and moving pointers from both ends inward, we can always process the side with the smaller current height. This works because the water level at that pointer is bottlenecked by its own side's maximum, given we already know the other side has a boundary at least as high.