# LeetCode – Trapping Rain Water (Java)

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

For example, given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.

Analysis

This problem is similar to Candy. It can be solve by scanning from both sides and then get the total.

Java Solution

```public int trap(int[] height) { int result = 0;   if(height==null || height.length<=2) return result;   int left[] = new int[height.length]; int right[]= new int[height.length];   //scan from left to right int max = height[0]; left[0] = height[0]; for(int i=1; i<height.length; i++){ if(height[i]<max){ left[i]=max; }else{ left[i]=height[i]; max = height[i]; } }   //scan from right to left max = height[height.length-1]; right[height.length-1]=height[height.length-1]; for(int i=height.length-2; i>=0; i--){ if(height[i]<max){ right[i]=max; }else{ right[i]=height[i]; max = height[i]; } }   //calculate totoal for(int i=0; i<height.length; i++){ result+= Math.min(left[i],right[i])-height[i]; }   return result; }```
Category >> Algorithms >> Interview
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
```<pre><code>
String foo = "bar";
</code></pre>
```
• Yinglei Zhang

This is a pretty cool solution. Thanks for sharing.

• Sim Prodigy

``` public class Solution { public int trap(int[] height) { if(height == null || height.length==0) return 0; int leftMax = 0, rightMax = 0, left = 0, max = 0; int right = height.length-1; while(left height[left] ? leftMax : height[left]; rightMax = rightMax > height[right] ? rightMax : height[right]; max += leftMax < rightMax ? leftMax - height[left++] : rightMax - height[right--]; } return max; } } ```

• Sim Prodigy

``` public class Solution { public int trap(int[] height) { int l = height.length; if(l < 3) return 0; int[] leftMax = new int[l]; int[] rightMax = new int[l]; int maxL = height[0]; int maxR = height[l-1]; for(int i = 0; i maxL ? height[i] : maxL; leftMax[i] = maxL; maxR = height[l-1-i] > maxR ? height[l-1-i] : maxR; rightMax[l-1-i] = maxR; } int waterTrapped = 0; for(int i = 0; i rightMax[i] ? rightMax[i] - height[i] : leftMax[i] - height[i]; } return waterTrapped; } } ```

• Vasyl Grygoryev

Constant O(1) space and O(n) time.
``` public class Solution { public int trap(int[] height) { if (height == null || height.length < 3) return 0;```

``` int result = 0; int left = 0; int right = height.length - 1; int maxLeft = 0; int maxRight = 0; int curHeight; while (left < right) { curHeight = Math.min(maxLeft, maxRight); if (maxLeft < maxRight) { result += Math.max(curHeight - height[left], 0); maxLeft = Math.max(height[left], maxLeft); left++; } else { result += Math.max(curHeight - height[right], 0); maxRight = Math.max(height[right], maxRight); right--; } } curHeight = Math.min(maxLeft, maxRight); result += Math.max(curHeight - height[right], 0); ```

``` return result; } } ```

• JY

sorry….my mistake….I meant constant space….

• JY

Is it possible to do it in constant time? it’s a FB interview question. Haven’t figured out how to do it…