Problem: Trapping RainWater
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.
Constraints: n == height.length 1 <= n <= 2 * 104 0 <= height[i] <= 105
class Solution {
public int trap(int[] height) {
int j = height.length-1;
int[] left = new int[height.length];
int[] right = new int[height.length];
int leftMax = 0;
int rightMax = 0;
int i = 0;
while(i<height.length){
leftMax = Math.max(leftMax,height[i]);
left[i] = leftMax;
i++;
rightMax = Math.max(rightMax,height[j]);
right[j] = rightMax;
j--;
}
int sum = 0;
for(i=0 ; i<height.length ; i++){
sum += Math.min(left[i], right[i]) -height[i];
}
return sum;
}
}