Skip to content

Commit 2a87bb3

Browse files
committed
LeetCode #11: Container With Most Water (refactor)
1 parent 76e85a7 commit 2a87bb3

2 files changed

Lines changed: 66 additions & 14 deletions

File tree

src/main/java/org/gd/leetcode/p0011/Solution.java

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package org.gd.leetcode.p0011;
22

3-
import org.gd.leetcode.common.Repeat;
43
import org.gd.leetcode.common.LeetCode;
4+
import org.gd.leetcode.common.Repeat;
55

66
/**
7-
* https://leetcode.com/problems/container-with-most-water/
7+
* <a href="https://leetcode.com/problems/container-with-most-water/">LeetCode #11: Container With Most Water</a>
8+
*
9+
* @see org.gd.leetcode.p0042.Solution
810
*/
911
@Repeat
1012
@LeetCode(
@@ -15,22 +17,31 @@
1517
LeetCode.Tags.ARRAY,
1618
LeetCode.Tags.TWO_POINTERS
1719
})
20+
@SuppressWarnings("JavadocReference")
1821
class Solution {
1922

20-
private static int max(int i1, int i2, int i3) { return Math.max(i1, Math.max(i2, i3)); }
23+
public int maxArea(int[] heights) {
24+
if (heights == null || heights.length < 2) {
25+
return 0;
26+
}
2127

22-
public int maxArea(int[] height) {
23-
switch (height.length) {
24-
case 0:
25-
case 1: return 0;
26-
case 2: return Math.min(height[0], height[1]);
27-
case 3: return max(height[0], height[1], height[2]);
28+
if (heights.length == 2) {
29+
return Math.max(0, Math.min(heights[0], heights[1]));
2830
}
29-
int i = 0, j = height.length - 1, max = Integer.MIN_VALUE, h;
30-
while (i < j) {
31-
max = Math.max(max, (j - i) * (h = Math.min(height[i], height[j])));
32-
while (height[i] <= h && i < j) i++;
33-
while (height[j] <= h && i < j) j--;
31+
32+
int leftIndex = 0;
33+
int rightIndex = heights.length - 1;
34+
int max = 0;
35+
while (leftIndex < rightIndex) {
36+
int height = Math.min(heights[leftIndex], heights[rightIndex]);
37+
int width = rightIndex - leftIndex;
38+
max = Math.max(max, width * height);
39+
while (heights[leftIndex] <= height && leftIndex < rightIndex) {
40+
leftIndex++;
41+
}
42+
while (heights[rightIndex] <= height && leftIndex < rightIndex) {
43+
rightIndex--;
44+
}
3445
}
3546
return max;
3647
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.gd.leetcode.p0011;
2+
3+
import lombok.NonNull;
4+
import lombok.SneakyThrows;
5+
import lombok.val;
6+
import org.junit.jupiter.api.DisplayName;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
10+
11+
import java.util.stream.Stream;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
15+
/**
16+
* Test for {@link org.gd.leetcode.p0011.Solution}.
17+
*
18+
* @author Horkhover Dmytro
19+
* @see Solution
20+
* @since 2022-09-09
21+
*/
22+
@DisplayName("LeetCode #11: Container With Most Water")
23+
class SolutionTest {
24+
25+
@NonNull
26+
private static Stream<Arguments> args() {
27+
return Stream.of(
28+
Arguments.of(new int[]{1, 8, 6, 2, 5, 4, 8, 3, 7}, 49),
29+
Arguments.of(new int[]{1, 1}, 1)
30+
);
31+
}
32+
33+
@ParameterizedTest
34+
@MethodSource("args")
35+
@DisplayName("Test for maxArea")
36+
@SneakyThrows
37+
void test_maxArea(int[] heights, int expected) {
38+
val solution = new Solution();
39+
assertThat(solution.maxArea(heights)).isEqualTo(expected);
40+
}
41+
}

0 commit comments

Comments
 (0)