@@ -14,17 +14,14 @@ public int maxArea(int[] height) {
14
14
int right = height .length - 1 ;
15
15
int max = calcWater (left , right );
16
16
17
- while (left < right ) {
18
- if (height [left ] < height [right ]) {
17
+ while (true ) {
18
+ if (height [left ] < height [right ])
19
19
left = findNextLeftPosition (left );
20
- if (right <= left ) break ;
21
- max = Math .max (max , calcWater (left , right ));
22
- }
23
- else {
20
+ else
24
21
right = findNextRightPosition (right );
25
- if ( right <= left ) break ;
26
- max = Math . max ( max , calcWater ( left , right )) ;
27
- }
22
+
23
+ if ( right <= left ) break ;
24
+ max = Math . max ( max , calcWater ( left , right ));
28
25
}
29
26
30
27
return max ;
@@ -36,13 +33,13 @@ private int calcWater(int left, int right) {
36
33
37
34
private int findNextLeftPosition (int left ) {
38
35
int prevHeight = height [left ];
39
- while (height [ left ] <= prevHeight && left < height . length - 1 ) left ++;
36
+ while (left < height . length - 1 && height [ left ] <= prevHeight ) left ++;
40
37
return left ;
41
38
}
42
39
43
40
private int findNextRightPosition (int right ) {
44
41
int prevHeight = height [right ];
45
- while (height [ right ] <= prevHeight && 0 < right ) right --;
42
+ while (0 < right && height [ right ] <= prevHeight ) right --;
46
43
return right ;
47
44
}
48
45
0 commit comments