|
1 | 1 | package joshua.leetcode.array.twopointers;
|
2 | 2 |
|
| 3 | +import joshua.leetcode.solutiontag.Stacks; |
| 4 | +import joshua.leetcode.solutiontag.TwoPointers; |
| 5 | +import sun.reflect.generics.reflectiveObjects.NotImplementedException; |
| 6 | + |
| 7 | +import java.util.Deque; |
| 8 | +import java.util.LinkedList; |
| 9 | + |
3 | 10 | /**
|
4 |
| - * Trapping Rain Water |
5 |
| - * |
6 |
| - * @see < a href="https://leetcode.com/problems/trapping-rain-water/">leetcode link</a> |
7 |
| - * @author joy |
| 11 | + * Trapping Rain Water |
8 | 12 | *
|
| 13 | + * @author joy |
| 14 | + * @see < a href="https://leetcode.com/problems/trapping-rain-water/">leetcode link</a> |
9 | 15 | */
|
10 | 16 | public abstract class TrappingRainWater {
|
11 | 17 |
|
12 |
| - /** |
13 |
| - * Given n non-negative integers representing an elevation map where the width of each bar is 1, |
14 |
| - * |
15 |
| - * compute how much water it is able to trap after raining. |
16 |
| - * |
17 |
| - * @param height |
18 |
| - * @return |
19 |
| - */ |
20 |
| - public abstract int trap(int[] height); |
| 18 | + /** |
| 19 | + * Given n non-negative integers representing an elevation map where the width of each bar is 1, |
| 20 | + * <p/> |
| 21 | + * compute how much water it is able to trap after raining. |
| 22 | + * <p/> |
| 23 | + * |
| 24 | + * For example, |
| 25 | + * Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. |
| 26 | + * |
| 27 | + * @param height |
| 28 | + * @return |
| 29 | + */ |
| 30 | + public abstract int trap(int[] height); |
| 31 | + |
| 32 | + @TwoPointers |
| 33 | + public static class Solution1 extends TrappingRainWater { |
| 34 | + |
| 35 | + @Override |
| 36 | + public int trap(int[] height) { |
| 37 | + throw new NotImplementedException(); |
| 38 | + } |
| 39 | + } |
21 | 40 |
|
22 |
| - /** |
23 |
| - * stack, two pointers |
24 |
| - * |
25 |
| - * @author joy |
26 |
| - * |
27 |
| - */ |
28 |
| - public static class Solution1 extends TrappingRainWater { |
| 41 | + /** |
| 42 | + * 使用栈,按元素从左到右,比栈顶元素小的话就入栈,直到比栈顶大的元素就开始对做出栈操作,每次计算两个元素之间水平方向 |
| 43 | + * 顶部聚集起来的水方块的个数 |
| 44 | + */ |
| 45 | + @Stacks |
| 46 | + public static class Solution2 extends TrappingRainWater { |
29 | 47 |
|
30 |
| - @Override |
31 |
| - public int trap(int[] height) { |
32 |
| - if (height == null || height.length <= 2) |
33 |
| - return 0; |
34 |
| - int leftIdx = 0, rightIdx = 1; |
35 |
| - int totalSum = 0; |
36 |
| - while (rightIdx < height.length) { |
37 |
| - int subSum = 0; |
38 |
| - while (rightIdx < height.length |
39 |
| - && height[rightIdx] < height[leftIdx]) { |
40 |
| - subSum += height[leftIdx] - height[rightIdx]; |
41 |
| - rightIdx++; |
42 |
| - } |
43 |
| - if (rightIdx < height.length) { |
44 |
| - totalSum += subSum; |
45 |
| - leftIdx = rightIdx; |
46 |
| - rightIdx++; |
47 |
| - } |
48 |
| - } |
49 |
| - /*if leftIdx==height.length-1, then we handled all the elements in array, |
50 |
| - * otherwise need to take care of the interval [leftIdx,height.Length-1]*/ |
51 |
| - while (leftIdx < height.length) { |
52 |
| - int temp = leftIdx + 1; |
53 |
| - int sum = 0; |
54 |
| - while (temp < height.length && height[temp] < height[temp - 1]) { |
55 |
| - sum += height[leftIdx] - height[temp]; |
56 |
| - temp++; |
57 |
| - } |
58 |
| - if (temp == height.length) |
59 |
| - break; |
60 |
| - while (temp < height.length && height[temp] >= height[temp - 1]) { |
61 |
| - sum += height[leftIdx] - height[temp]; |
62 |
| - temp++; |
63 |
| - } |
64 |
| - sum -= (height[leftIdx] - height[temp - 1]) |
65 |
| - * (temp - 1 - leftIdx); |
66 |
| - totalSum += sum; |
67 |
| - if (temp < height.length) { |
68 |
| - leftIdx = temp - 1; |
69 |
| - } else { |
70 |
| - break; |
71 |
| - } |
72 |
| - } |
73 |
| - return totalSum; |
74 |
| - } |
75 |
| - } |
| 48 | + @Override |
| 49 | + public int trap(int[] height) { |
| 50 | + if (height == null || height.length == 0) { |
| 51 | + return 0; |
| 52 | + } |
| 53 | + Deque<Integer> stack = new LinkedList<Integer>(); |
| 54 | + // stack stores the index of element. |
| 55 | + int water = 0; |
| 56 | + stack.push(0); |
| 57 | + for (int i = 1; i < height.length; i++) { |
| 58 | + if(stack.isEmpty() || height[i] < height[stack.peek()]) { |
| 59 | + stack.push(i); |
| 60 | + continue; |
| 61 | + } |
| 62 | + int bottom = height[stack.pop()]; |
| 63 | + while (!stack.isEmpty()) { |
| 64 | + if ( height[stack.peek()] <= height[i]) { |
| 65 | + int idx = stack.pop(); |
| 66 | + water += (i - idx - 1) * (height[idx] - bottom); |
| 67 | + bottom = height[idx]; |
| 68 | + } else { |
| 69 | + break; |
| 70 | + } |
| 71 | + } |
| 72 | + stack.push(i); |
| 73 | + } |
| 74 | + return water; |
| 75 | + } |
| 76 | + } |
76 | 77 | }
|
0 commit comments