> For the complete documentation index, see [llms.txt](https://zcjian.gitbook.io/project/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zcjian.gitbook.io/project/array/trapping-rain-water.md).

# Trapping Rain Water

42

## Two Pointers

```python
class Solution:
    def trap(self, h):
        """
        :type h: List[int]
        :rtype: int
        """
        left = 0
        right = len(h) -1
        leftMax, rightMax = 0, 0
        res = 0
        
        while left < right:
            if h[left] < h[right]:
                if h[left] > leftMax:
                    leftMax = h[left]
                else:
                    res += (leftMax - h[left])    
                left += 1
                
            if h[left] >= h[right]:
                
                if h[right] > rightMax:
                    rightMax = h[right]
                else:
                    res += (rightMax - h[right])
                right -=1
        return res
```
