Skip to content

Commit 6971f27

Browse files
committed
trapping rain water
1 parent 85de565 commit 6971f27

File tree

3 files changed

+90
-74
lines changed

3 files changed

+90
-74
lines changed
Lines changed: 67 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,77 @@
11
package joshua.leetcode.array.twopointers;
22

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+
310
/**
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
812
*
13+
* @author joy
14+
* @see < a href="https://leetcode.com/problems/trapping-rain-water/">leetcode link</a>
915
*/
1016
public abstract class TrappingRainWater {
1117

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+
}
2140

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 {
2947

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+
}
7677
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package joshua.leetcode.solutiontag;
2+
3+
/**
4+
* Created by joshu on 2016/5/23.
5+
*/
6+
public @interface Stacks {
7+
}

src/test/java/joshua/leetcode/array/twopointers/TrappingRainWaterTest.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,27 @@
22

33
import static org.junit.Assert.*;
44

5+
import com.google.common.collect.Maps;
6+
import org.junit.Before;
57
import org.junit.Test;
68

9+
import java.util.Map;
10+
711
public class TrappingRainWaterTest {
812

13+
private Map<int[], Integer> cases = Maps.newHashMap();
14+
15+
@Before
16+
public void setUp() {
17+
cases.put(new int[]{0,1,0,2,1,0,1,3,2,1,2,1}, 6);
18+
}
19+
920
@Test
10-
public void testSolution1() {
11-
TrappingRainWater sol=new TrappingRainWater.Solution1();
12-
// int[] height=new int[]{0,1,0,2,1,0,1,3,2,1,2,1};
13-
int[] height=new int[]{9,6,8,8,5,6,3};
14-
assertEquals(6,sol.trap(height));
15-
// int[] height=new int[]{4,2,3};
16-
// assertEquals(1,sol.trap(height));
17-
21+
public void testSolution2() {
22+
TrappingRainWater sol=new TrappingRainWater.Solution2();
23+
for (int[] key : cases.keySet()) {
24+
assertEquals((int)cases.get(key), sol.trap(key));
25+
}
1826
}
1927

2028
}

0 commit comments

Comments
 (0)