Skip to content

Commit 0a84d63

Browse files
[N-0] refactor 11
1 parent 5f57133 commit 0a84d63

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed

src/main/java/com/fishercoder/solutions/_11.java

+15-16
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,23 @@
1212
*/
1313
public class _11 {
1414

15-
public int maxArea(int[] height) {
16-
int max = Integer.MIN_VALUE;
17-
int len = height.length;
18-
int i = 0;
19-
int j = len - 1;
20-
while (i < j) {
21-
if (Math.min(height[i], height[j]) * (j - i) > max) {
22-
max = Math.min(height[i], height[j]) * (j - i);
23-
}
24-
if (height[i] <= height[j]) {
25-
// we need to find the shorter one,
26-
// then calculate its area
27-
i++;
28-
} else {
29-
j--;
15+
public static class Solution1 {
16+
public int maxArea(int[] height) {
17+
int max = 0;
18+
int i = 0;
19+
int j = height.length - 1;
20+
while (i < j) {
21+
max = Math.max(Math.min(height[i], height[j]) * (j - i), max);
22+
if (height[i] <= height[j]) {
23+
// we need to find the shorter one,
24+
// then calculate its area
25+
i++;
26+
} else {
27+
j--;
28+
}
3029
}
30+
return max;
3131
}
32-
return max;
3332
}
3433

3534
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._11;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.Assert.assertEquals;
8+
9+
public class _11Test {
10+
private static _11.Solution1 solution1;
11+
private static int[] height;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _11.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
height = new int[]{1, 1};
21+
assertEquals(1, solution1.maxArea(height));
22+
}
23+
24+
}

0 commit comments

Comments
 (0)