File tree Expand file tree Collapse file tree
main/java/com/fishercoder/solutions/firstthousand
test/java/com/fishercoder/firstthousand Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22
33public class _11 {
44 public static class Solution1 {
5- /*
6- * Time: O(n^2)
7- * This brute force solution is NOT accepted on LeetCode due to TLE.
8- */
9- public int maxArea (int [] height ) {
10- int maxArea = 0 ;
11- for (int left = 0 ; left < height .length - 1 ; left ++) {
12- for (int right = height .length - 1 ; left < right ; right --) {
13- int area = (right - left ) * Math .min (height [left ], height [right ]);
14- maxArea = Math .max (maxArea , area );
15- }
16- }
17- return maxArea ;
18- }
19- }
20-
21- public static class Solution2 {
225 /*
236 * Two pointer technique.
247 * Well explained here: https://leetcode.com/problems/container-with-most-water/discuss/6100/Simple-and-clear-proofexplanation
Original file line number Diff line number Diff line change 88
99public class _11Test {
1010 private _11 .Solution1 solution1 ;
11- private _11 .Solution2 solution2 ;
1211 private static int [] height ;
1312 private static int expected ;
1413
1514 @ BeforeEach
1615 public void setup () {
1716 solution1 = new _11 .Solution1 ();
18- solution2 = new _11 .Solution2 ();
1917 }
2018
2119 @ Test
2220 public void test1 () {
2321 height = new int [] {1 , 1 };
2422 expected = 1 ;
2523 assertEquals (expected , solution1 .maxArea (height ));
26- assertEquals (expected , solution2 .maxArea (height ));
2724 }
2825}
You can’t perform that action at this time.
0 commit comments