1class Solution {2public int trap(int[] height) {3int left = 0;4int right = height.length - 1;56int l_max = height[left];7int r_max = height[right];8int res = 0;910while (left < right) {11if (l_max < r_max) {12left += 1;13l_max = Math.max(l_max, height[left]);14res += l_max - height[left];15} else {16right -= 1;17r_max = Math.max(r_max, height[right]);18res += r_max - height[right];19}20}2122return res;23}24}