1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
public class TrappingRainWater { public int trap(int[] height) { if (height == null || height.length < 3) { return 0; } int[] maxl = new int[height.length]; int[] maxr = new int[height.length]; int maxlv = height[0]; int maxrv = height[height.length - 1]; for (int i = 1; i < height.length - 1; i++) { maxl[i] = maxlv; maxlv = Math.max(maxlv, height[i]); int t = height.length - i - 1; maxr[t] = maxrv; maxrv = Math.max(maxrv, height[t]); } int result = 0; for (int i = 1; i < height.length - 1; i++) { int crt = Math.min(maxl[i], maxr[i]) - height[i]; result += Math.max(0, crt); } return result; } }
|