Skip to content

Commit 9a1d347

Browse files
committed
chore: Leetcode
1 parent cc853e7 commit 9a1d347

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package algorithm_sites.leetcode2;
2+
3+
public class LC_00011_ContainerWithMostWater {
4+
5+
public int maxArea(int[] heights) {
6+
int result = 0;
7+
8+
int front = 0;
9+
int rear = heights.length-1;
10+
while (front < rear) {
11+
int width = rear - front;
12+
int height = Math.min(heights[front], heights[rear]);
13+
result = Math.max(result, width * height);
14+
15+
if (heights[front] > heights[rear]) {
16+
--rear;
17+
} else {
18+
++front;
19+
}
20+
}
21+
22+
return result;
23+
}
24+
25+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package algorithm_sites.leetcode2;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class LC_00011_ContainerWithMostWaterTest {
8+
9+
private LC_00011_ContainerWithMostWater lc = new LC_00011_ContainerWithMostWater();
10+
11+
@Test
12+
void solutionTest1() {
13+
int result = lc.maxArea(new int[]{1, 8, 6, 2, 5, 4, 8, 3, 7});
14+
assertThat(result).isEqualTo(49);
15+
}
16+
17+
@Test
18+
void solutionTest2() {
19+
int result = lc.maxArea(new int[]{1,1});
20+
assertThat(result).isEqualTo(1);
21+
}
22+
23+
@Test
24+
void solutionTest3() {
25+
int result = lc.maxArea(new int[]{2,3,10,5,7,8,9});
26+
assertThat(result).isEqualTo(36);
27+
}
28+
29+
}

0 commit comments

Comments
 (0)