Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.

&nbsp;
Example 1:

Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6
Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.


Example 2:

Input: height = [4,2,0,3,2,5]
Output: 9


&nbsp;
Constraints:


	n == height.length
	1 &lt;= n &lt;= 2 * 104
	0 &lt;= height[i] &lt;= 105


class Solution:
    def trap(self, a: List[int]) -> int:
        l=0
        r=len(a)-1
        maxl=0
        maxr=0
        res=0

        while (l<=r):
            if a[l]<=a[r]:
                if a[l]>=maxl: maxl=a[l] #update maxl if a[l] is >=
                else: res+=maxl-a[l] #adding captured water when maxl>a[l]
                l+=1
            else:
                if a[r]>=maxr: maxr=a[r]
                else: res+=maxr-a[r]
                r-=1
        return res
class Solution {
    public int trap(int[] height) {
        int left = 0;
        int right = height.length - 1;
        
        int l_max = height[left];
        int r_max = height[right];
        int res = 0;
        
        while(left < right) {
            if(l_max < r_max) {
                left+=1;
                l_max = Math.max(l_max, height[left]);
                res += l_max - height[left];
            }
            else{
                right-=1;
                r_max = Math.max(r_max, height[right]);
                res += r_max - height[right];
            }
        }
        
        return res;
    }
}
class Solution {
public:
    int trap(vector<int>& height) {
        int left=0,right=height.size()-1;
        int maxleft=0,maxright=0;
        int res=0;
        while(left<=right){
            if(height[left]<=height[right]){
                if(height[left]>=maxleft)maxleft=height[left];
                else res += maxleft-height[left];
                left++;
            }else{
                 if(height[right]>=maxright)maxright=height[right];
                else res += maxright-height[right];
                right--;
            }
        }
        return res;
    }
};
var trap = function(height) {
    let left = 0, right = height.length - 1;
    let hiLevel = 0, water = 0;
    while(left <= right) {
        let loLevel = height[height[left] < height[right] ? left++ : right--];
        hiLevel = Math.max(hiLevel, loLevel);
        water += hiLevel - loLevel ;
    }
    return water;
};
Trapping Rain Water