-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMostWater2.java
More file actions
45 lines (44 loc) · 1.21 KB
/
Copy pathMostWater2.java
File metadata and controls
45 lines (44 loc) · 1.21 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
package AlgorithmTest;
/*
*
* */
public class MostWater2 {
public static void main(String[] args) {
int [] numbers={1,8,6,2,5,4,8,3,7};
maxArea(numbers);
}
public static int maxArea(int[] height) {
boolean flag=true;
int result=0;
int temp=1;
int startIndex=-1;
int endIndex=-1;
while(flag){
for(int i=0;i<height.length;i++){
if(height[i]>=temp&&startIndex==-1){
startIndex=i;
for(int j=height.length-1;j>i;j--){
if(height[j]>=temp){
endIndex=j;
if(temp*(endIndex-startIndex)>result){
result=temp*(endIndex-startIndex);
}
temp++;
break;
}
}
if(endIndex==-1){
flag=false;
}
break;
}
}
if (startIndex==-1){
break;
}
startIndex=-1;
endIndex=-1;
}
return result;
}
}