O(N) : 60 ms, faster than 48.07% of Python3 online submissions for Trapping Rain Water.
classSolution:deftrap(self,height)->int:size=len(height)ifsize==0:return0# left
left=[0]*sizeleftmax=height[0]foriinrange(size):ifleftmax>height[i]:left[i]=leftmax-height[i]else:leftmax=height[i]# right
right=[0]*sizerightmax=height[-1]foriinrange(size):ifrightmax>height[size-1-i]:right[size-1-i]=rightmax-height[size-1-i]else:rightmax=height[size-1-i]# result
result=0foriinrange(size):result+=min(right[i],left[i])returnresult