class Solution { public int trap(int[] height) { int sum=0; int l=0; int r=height.length-1; int left=0; int right=0; while(l<=r) { if(left<=right) { if(height[l]>left) left=height[l]; else sum+=left-height[l]; l++; } else { if(height[r]>right) right=height[r]; else sum+=right-height[r]; r--; } } return sum; } }