File tree Expand file tree Collapse file tree
src/main/java/interview/google Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package interview .google ;
2+
3+ import java .util .ArrayList ;
4+
5+ import org .junit .Test ;
6+
7+ import junit .framework .TestCase ;
8+
9+ /**
10+ * Link: https://www.interviewbit.com/problems/container-with-most-water/
11+ *
12+ * @author shivam.maharshi
13+ */
14+ public class ContainerWithMostWater extends TestCase {
15+
16+ @ Test
17+ public static void test () {
18+ ArrayList <Integer > l = new ArrayList <>();
19+ l .add (1 );
20+ l .add (5 );
21+ l .add (4 );
22+ l .add (3 );
23+ assertEquals (6 , maxArea (l ));
24+ }
25+
26+ public static int maxArea (ArrayList <Integer > a ) {
27+ if (a == null || a .size () == 0 )
28+ return 0 ;
29+ int r = 0 , x = 0 , y = a .size () - 1 ;
30+ while (x < y ) {
31+ r = Math .max (r , Math .min (a .get (y ), a .get (x )) * (y - x ));
32+ if (a .get (y ) < a .get (x ))
33+ y --;
34+ else
35+ x ++;
36+ }
37+ return r ;
38+ }
39+
40+ }
You can’t perform that action at this time.
0 commit comments