-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainerWithMostWater.java
More file actions
50 lines (46 loc) · 1.5 KB
/
ContainerWithMostWater.java
File metadata and controls
50 lines (46 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.leetcode;
/**
* Created by jamylu on 2018/1/3.
* leetcode011.
* 给定n个非负整数a1 a2。其中每个表示坐标点(i,ai)。
* n垂直的线是这样画的,直线i的两个端点在(i,ai)和(i,0)中,
* 找到两条直线,它们与x轴一起构成一个容器,这样容器就包含了最多的水。
*/
public class ContainerWithMostWater {
public static void main(String[] args) {
int[] height = {1, 8, 6, 2, 5, 4, 8, 3, 7};
System.out.println(mostWater(height));
System.out.println(maxArea(height));
}
//双指针法,一前一后向中间靠拢
public static int mostWater(int[] height) {
int i = 0;
int j = height.length - 1;
int max = 0;
int current;
while (j > i) {
current = (j - i) * Math.min(height[i], height[j]);
if (current > max) {
max = current;
}
//影响容器盛水的是矮的那个
if (height[i] < height[j]) {
i++;
} else {
j--;
}
}
return max;
}
//暴力法,超时
public static int maxArea(int[] height) {
int max = 0;
for (int i = 0; i < height.length - 1; i++) {
for (int j = i + 1; j < height.length; j++)
if ((j - i) * Math.min(height[i], height[j]) > max) {
max = (j - i) * Math.min(height[i], height[j]);
}
}
return max;
}
}