Skip to content

Commit c46a5d6

Browse files
committed
fd
1 parent 2b11bc6 commit c46a5d6

File tree

3 files changed

+12
-20
lines changed

3 files changed

+12
-20
lines changed

leetcode/solution/src/ContainerWithMostWater.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* 一种O(n)的方法是对于区间[left, right],假如height[left] < height[right],则我们可以认定[left, right - 1],[left, right - 2] ...
44
* 都不会比[left, right]装的水更多。原因是木桶的短板没变,所以水平面不会变,但是宽度减小了。
55
* 所以我们要继续尝试的方向是从较短的一方推进,寄希望于其之后的板子能高一点
6+
* 如果两边一样高,那随便推进哪一边都行,因为假如之后还能装更多水,一定不会包含这两边任意一块。如果包含了,水量不会超过当前。
67
*/
78
public class ContainerWithMostWater {
89

leetcode/solution/src/ThreeSum.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,11 @@
33
import java.util.List;
44

55
/**
6-
* TestCase
7-
* [-4,-2,1,-5,-4,-4,4,-2,0,4,0,-2,3,1,-5,0]
86
* 这题重要的是查重
7+
* 两重循环都要查重
98
*/
109
public class ThreeSum {
1110

12-
/**
13-
* 最底下那个for循环别给j < k掉了
14-
*/
15-
1611
// 耗时30ms
1712
public List<List<Integer>> threeSum(int[] nums) {
1813
Arrays.sort(nums);

leetcode/src/Main.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@
22

33
public class Main {
44

5-
public int trap(int[] height) {
6-
int[] left = new int[height.length];
7-
int[] right = new int[height.length];
8-
9-
for (int i = 1; i < height.length; i++) {
10-
left[i] = Math.max(left[i - 1], height[i - 1]);
11-
}
12-
for (int i = height.length - 2; i >= 0; i--) {
13-
right[i] = Math.max(right[i + 1], height[i + 1]);
14-
}
15-
int rain = 0;
16-
for (int i = 1; i < height.length - 1; i++) {
17-
rain += Math.max(0, Math.min(left[i], right[i]) - height[i]);
5+
public int maxArea(int[] height) {
6+
int max = 0;
7+
for (int i = 0, j = height.length - 1; i < j; ) {
8+
max = Math.max(max, Math.min(height[i], height[j]) * (j - i));
9+
if (height[i] < height[j]) {
10+
i++;
11+
} else {
12+
j--;
13+
}
1814
}
19-
return rain;
15+
return max;
2016
}
2117

2218
public static void main(String[] args) {

0 commit comments

Comments
 (0)