Skip to content

Commit c96a988

Browse files
add trappingRainWater
1 parent 3e63c19 commit c96a988

12 files changed

+239
-196
lines changed

.idea/workspace.xml

Lines changed: 138 additions & 195 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
Binary file not shown.
-972 Bytes
Binary file not shown.
-985 Bytes
Binary file not shown.
-948 Bytes
Binary file not shown.
-1018 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.

src/leetcode/LargestRectangleHistogram.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package leetcode;
22

33
import org.junit.Test;
4-
54
import java.util.Stack;
65

76
/**

src/leetcode/TrappingRainWater1.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package leetcode;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* @author zhangyu
7+
* @version V1.0
8+
* @ClassName: TrappingRainWater1
9+
* @Description: 找到凹槽能装多少水问题!(brute force)暴力算法
10+
* @date 2019/1/18
11+
**/
12+
13+
14+
public class TrappingRainWater1 {
15+
@Test
16+
public void fun() {
17+
int arr[] = {1, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1};
18+
int maxWater = trappingRainWater(arr);
19+
System.out.println(maxWater);
20+
}
21+
22+
private int trappingRainWater(int[] arr) {
23+
// 如果数组长度小于3,就返回0
24+
if (arr == null || arr.length < 3) {
25+
return 0;
26+
}
27+
int result = 0;
28+
int size = arr.length;
29+
for (int i = 0; i < size - 1; i++) {
30+
int maxLeft = 0;
31+
int maxRight = 0;
32+
// 找出i前面的最大的一个数
33+
for (int j = i; j >= 0; j--) {
34+
maxLeft = Math.max(maxLeft, arr[j]);
35+
}
36+
// 找出i后面最大那个数
37+
for (int j = i; j < arr.length; j++) {
38+
maxRight = Math.max(maxRight, arr[j]);
39+
}
40+
// tarp就是他们直接的最小数和i的差
41+
result += Math.min(maxLeft, maxRight) - arr[i];
42+
}
43+
return result;
44+
}
45+
}

src/leetcode/TrappingRainWater2.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package leetcode;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* @author zhangyu
7+
* @version V2.0
8+
* @ClassName: TrappingRainWater1
9+
* @Description: 找到凹槽能装多少水问题!采用效率非常高一种算法,双向指针!
10+
* @date 2019/1/18
11+
**/
12+
13+
14+
public class TrappingRainWater2 {
15+
@Test
16+
public void fun() {
17+
int arr[] = {1, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1};
18+
int maxWater = trappingRainWater(arr);
19+
System.out.println(maxWater);
20+
}
21+
22+
private int trappingRainWater(int[] arr) {
23+
// 如果数组长度小于3,就返回0
24+
if (arr == null || arr.length < 3) {
25+
return 0;
26+
}
27+
int result = 0;
28+
int left = 0;
29+
int right = arr.length - 1;
30+
int leftMax = 0;
31+
int rightMax = 0;
32+
// 两边双指针,它们各自向中间压缩
33+
while (left < right) {
34+
// 如果左边小于右边就从左边开始
35+
if (arr[left] < arr[right]) {
36+
if (arr[left] > leftMax) {
37+
leftMax = arr[left];
38+
} else {
39+
result += leftMax - arr[left];
40+
}
41+
// 指针+1
42+
left++;
43+
} else {
44+
if (arr[right] < rightMax) {
45+
rightMax = arr[rightMax];
46+
} else {
47+
result += rightMax - arr[right];
48+
}
49+
// 指针-1
50+
right--;
51+
}
52+
}
53+
return result;
54+
}
55+
}
56+

0 commit comments

Comments
 (0)