-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainMostWater.java
More file actions
65 lines (61 loc) · 1.47 KB
/
containMostWater.java
File metadata and controls
65 lines (61 loc) · 1.47 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* Created by Administrator on 2016/5/16.
*/
public class containMostWater {
public static void main(String args[])
{
int max;
containMostWater water = new containMostWater();
int arr[] ={1,2,3,9,8,6,5,1};
max = water.maxArea(arr);
System.out.print(max);
// System.out.println(1);
}
//
// int maxArea(int[] height) {
//
// int min;
// int max = 0;
// int tmp = 0;
// int pre = 0 ;
// int after = 1;
// int length = height.length;
//
// while(pre<length-1) {
// while (after < length) {
//// System.out.println(after);
// min= Math.min(height[pre],height[after]);
// tmp = min*(after-pre);
// if(tmp > max)
// {
// max = tmp;
// }
// after++;
// }
// pre++;
// after =1;
// }
//
// return max;
//
// }
int maxArea(int[] height)
{
int pre = 0;
int tmp= 0;
int max = 0;
int after = height.length-1;
while(pre<after)
{
tmp = (after-pre)*Math.min(height[pre],height[after]);
max = (tmp>max)? tmp:max;
if(height[pre]<height[after])
{
pre++;
}else {
after--;
}
}
return max;
}
}