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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
| package com.leetcode;
public class TrappingRainWater42 { public static void main(String[] args) { System.out.println(new Solution3().trap(new int[]{9, 8, 9, 5, 8, 8, 8, 0, 4})); }
private static class Solution { public int trap(int[] height) { return doTrap(height, 0, height.length - 1); }
private int doTrap(int[] height, int start, int end) { if (start >= end - 1) return 0; int heighestIndex = findMaxIndex(height, start, end, 0); int totalWater = 0; int leftSecondHeightIndex = findMaxIndex(height, start, heighestIndex - 1, 0); int rightSecondHeightIndex = findMaxIndex(height, heighestIndex + 1, end, 1); if (leftSecondHeightIndex != -1) { for (int i = leftSecondHeightIndex + 1; i <= heighestIndex - 1; i++) { totalWater += height[leftSecondHeightIndex] - height[i]; } totalWater += doTrap(height, start, leftSecondHeightIndex); } if (rightSecondHeightIndex != -1) { for (int i = heighestIndex + 1; i <= rightSecondHeightIndex - 1; i++) { totalWater += height[rightSecondHeightIndex] - height[i]; } totalWater += doTrap(height, rightSecondHeightIndex, end); } return totalWater; }
private int findMaxIndex(int[] height, int start, int end, int direction) { if (start >= end) return -1; int tmp; if (direction == 0) { tmp = start; for (int i = start; i <= end; i++) { if (height[tmp] < height[i]) tmp = i; } } else { tmp = end; for (int i = end; i >= start; i--) { if (height[tmp] < height[i]) tmp = i; } } return tmp; } }
private static class Solution2 { public int trap(int[] height) { int totalWater = 0; int startPoint = 0, endPoint = height.length - 1; int recordStart, recordEnd; while (startPoint < endPoint) { recordStart = startPoint; recordEnd = endPoint; while (startPoint < endPoint - 1 && height[recordStart] >= height[startPoint + 1]) startPoint++; startPoint++; if (startPoint >= endPoint && height[recordStart] > height[startPoint]) startPoint = recordStart; while (startPoint < endPoint - 1 && height[recordEnd] >= height[endPoint - 1]) endPoint--; endPoint--; if (startPoint >= endPoint && height[endPoint] < height[recordEnd]) endPoint = recordEnd; for (int i = recordStart + 1; i < startPoint; i++) { totalWater += height[recordStart] - height[i]; } for (int i = endPoint + 1; i < recordEnd; i++) { totalWater += height[recordEnd] - height[i]; } } return totalWater; } }
private static class Solution3 { public int trap(int[] height) { int totalWater = 0; int startPoint = 0, endPoint = height.length - 1; int recordStart, recordEnd; int maxIndex = 0; for (int i = 1; i < height.length; i++) { if (height[maxIndex] < height[i]) maxIndex = i; } while (startPoint < endPoint) { recordStart = startPoint; recordEnd = endPoint; while (startPoint < maxIndex && height[recordStart] >= height[startPoint + 1]) startPoint++; if (startPoint < maxIndex) startPoint++; while (endPoint > maxIndex && height[recordEnd] >= height[endPoint - 1]) endPoint--; if (endPoint > maxIndex) endPoint--; for (int i = recordStart + 1; i < startPoint; i++) { totalWater += height[recordStart] - height[i]; } for (int i = endPoint + 1; i < recordEnd; i++) { totalWater += height[recordEnd] - height[i]; } } return totalWater; } }
}
|