| 1 | //Solution of traping rain water in java |
| 2 | class Solution { |
| 3 | public int trap(int[] height) { |
| 4 | //create 2 temprory array |
| 5 | int a[] = new int[height.length]; |
| 6 | int b[] = new int[height.length]; |
| 7 | int min = Integer.MIN_VALUE; |
| 8 | //first we are finfig min hight while traversing height array form left to right |
| 9 | for(int i=0;i<height.length;i++){ |
| 10 | if(min<height[i]){ |
| 11 | min=height[i]; |
| 12 | } |
| 13 | a[i]=min; |
| 14 | } |
| 15 | //fnow we are finfig min hight while traversing height array form right to rleft |
| 16 | min = Integer.MIN_VALUE; |
| 17 | for(int i=height.length-1;i>=0;i--){ |
| 18 | if(min<height[i]){ |
| 19 | min=height[i]; |
| 20 | } |
| 21 | b[i]=min; |
| 22 | } |
| 23 | int total_water=0; |
| 24 | //at last we will add min number from temrory array and minus hight which is not coming in use while storing water |
| 25 | for(int i=0;i<height.length;i++){ |
| 26 | total_water+= Math.min(a[i],b[i])-height[i]; |
| 27 | } |
| 28 | return total_water; |
| 29 | |
| 30 | } |
| 31 | } |
| 32 |
nothing calls this directly
no outgoing calls
no test coverage detected