Given n non-negative integers representing an elevation map where the width of each bar are 1, compute how much WA ter It is the able to trap after raining.
For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1] , return 6 .
The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of the Rain Water (blue section) is being trapped. Thanks Marcos for contributing this image!
Reference: Dai Fangqin ([email protected]) Https://github.com/soulmachine/leetcode
public class Solution {public int trap (int[] A) { int sum = 0; int Maxleft [] = new Int[a.length]; int Maxright [] = new int [a.length]; for (int i=1;i<a.length;i++) { Maxleft[i] = Math.max (Maxleft[i-1], a[i-1]); Maxright[a.length-1-i] = Math.max (Maxright[a.length-i], a[a.length-i]); } for (int i=0;i<a.length;i++) { int height = math.min (maxleft[i],maxright[i]); if (Height>=a[i]) { sum + = Height-a[i];} } return sum; }}
[Leetcode] Trapping Rain Water