|
| 1 | +import java.util.Deque; |
| 2 | +import java.util.LinkedList; |
| 3 | + |
| 4 | +/** |
| 5 | + * @author: wangjunchao(王俊超) |
| 6 | + * @time: 2018-09-28 15:20 |
| 7 | + **/ |
| 8 | +class Solution { |
| 9 | + |
| 10 | + public int trap(int[] height) { |
| 11 | + return trap4(height); |
| 12 | + } |
| 13 | + |
| 14 | + public int trap1(int[] height) { |
| 15 | + int ans = 0; |
| 16 | + int size = height.length; |
| 17 | + for (int i = 1; i < size - 1; i++) { |
| 18 | + int maxLeft = 0; |
| 19 | + int maxRight = 0; |
| 20 | + for (int j = i; j >= 0; j--) { //Search the left part for max bar size |
| 21 | + maxLeft = Math.max(maxLeft, height[j]); |
| 22 | + } |
| 23 | + for (int j = i; j < size; j++) { //Search the right part for max bar size |
| 24 | + maxRight = Math.max(maxRight, height[j]); |
| 25 | + } |
| 26 | + |
| 27 | + // TODO 原理是什么? |
| 28 | + ans += Math.min(maxLeft, maxRight) - height[i]; |
| 29 | + } |
| 30 | + return ans; |
| 31 | + } |
| 32 | + |
| 33 | + int trap2(int[] height) { |
| 34 | + if (height == null || height.length == 0) { |
| 35 | + return 0; |
| 36 | + } |
| 37 | + int ans = 0; |
| 38 | + int size = height.length; |
| 39 | + int[] leftMax = new int[size]; |
| 40 | + int[] rightMax = new int[size]; |
| 41 | + |
| 42 | + leftMax[0] = height[0]; |
| 43 | + for (int i = 1; i < size; i++) { |
| 44 | + leftMax[i] = Math.max(height[i], leftMax[i - 1]); |
| 45 | + } |
| 46 | + |
| 47 | + rightMax[size - 1] = height[size - 1]; |
| 48 | + for (int i = size - 2; i >= 0; i--) { |
| 49 | + rightMax[i] = Math.max(height[i], rightMax[i + 1]); |
| 50 | + } |
| 51 | + |
| 52 | + for (int i = 1; i < size - 1; i++) { |
| 53 | + ans += Math.min(leftMax[i], rightMax[i]) - height[i]; |
| 54 | + } |
| 55 | + return ans; |
| 56 | + } |
| 57 | + |
| 58 | + int trap3(int[] height) { |
| 59 | + int ans = 0, current = 0; |
| 60 | + Deque<Integer> st = new LinkedList<>(); |
| 61 | + while (current < height.length) { |
| 62 | + while (!st.isEmpty() && height[current] > height[st.getFirst()]) { |
| 63 | + int top = st.peek(); |
| 64 | + st.pop(); |
| 65 | + if (st.isEmpty()) { |
| 66 | + break; |
| 67 | + } |
| 68 | + int distance = current - st.peek() - 1; |
| 69 | + int boundedHeight = Math.min(height[current], height[st.peek()]) - height[top]; |
| 70 | + ans += distance * boundedHeight; |
| 71 | + } |
| 72 | + st.push(current++); |
| 73 | + } |
| 74 | + return ans; |
| 75 | + } |
| 76 | + |
| 77 | + int trap4(int[] height) { |
| 78 | + int left = 0; |
| 79 | + int right = height.length - 1; |
| 80 | + int ans = 0; |
| 81 | + int leftMax = 0; |
| 82 | + int rightMax = 0; |
| 83 | + |
| 84 | + while (left < right) { |
| 85 | + if (height[left] < height[right]) { |
| 86 | + if (height[left] >= leftMax) { |
| 87 | + leftMax = height[left]; |
| 88 | + } else { |
| 89 | + ans += leftMax - height[left]; |
| 90 | + } |
| 91 | + |
| 92 | + ++left; |
| 93 | + } else { |
| 94 | + if (height[right] >= rightMax) { |
| 95 | + rightMax = height[right]; |
| 96 | + } else { |
| 97 | + ans += rightMax - height[right]; |
| 98 | + } |
| 99 | + |
| 100 | + --right; |
| 101 | + } |
| 102 | + } |
| 103 | + return ans; |
| 104 | + } |
| 105 | +} |
0 commit comments