Skip to content

Commit b102b0c

Browse files
committed
Refator problem 11
1 parent 317d750 commit b102b0c

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

src/main/java/io/github/juchanei/leetcodeJava/ContainerWithMostWater.java

+8-11
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,14 @@ public int maxArea(int[] height) {
1414
int right = height.length - 1;
1515
int max = calcWater(left, right);
1616

17-
while (left < right) {
18-
if (height[left] < height[right]) {
17+
while (true) {
18+
if (height[left] < height[right])
1919
left = findNextLeftPosition(left);
20-
if (right <= left) break;
21-
max = Math.max(max, calcWater(left, right));
22-
}
23-
else {
20+
else
2421
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));
2825
}
2926

3027
return max;
@@ -36,13 +33,13 @@ private int calcWater(int left, int right) {
3633

3734
private int findNextLeftPosition(int left) {
3835
int prevHeight = height[left];
39-
while (height[left] <= prevHeight && left < height.length - 1) left++;
36+
while (left < height.length - 1 && height[left] <= prevHeight) left++;
4037
return left;
4138
}
4239

4340
private int findNextRightPosition(int right) {
4441
int prevHeight = height[right];
45-
while (height[right] <= prevHeight && 0 < right) right--;
42+
while (0 < right && height[right] <= prevHeight) right--;
4643
return right;
4744
}
4845

0 commit comments

Comments
 (0)