package leetcode;

/**
 * https://leetcode.com/problems/maximum-length-of-repeated-subarray/
 */
public class Problem718 {
    public int findLength(int[] A, int[] B) {
        int[][] memo = new int[A.length + 1][B.length + 1];
        IntRef max = new IntRef();
        return findLength(A, B, 0, 0, max, memo);
//        int max = 0;
//        int[][] memo = new int[A.length + 1][B.length + 1];
//        for (int i = A.length - 1; i >= 0; i--) {
//            for (int j = B.length - 1; j >= 0; j--) {
//                if (A[i] == B[j]) {
//                    memo[i][j] = memo[i + 1][j + 1] + 1;
//                    max = Math.max(max, memo[i][j]);
//                }
//            }
//        }
//        return max;
    }

    private static class IntRef {
        private int val;
    }

    private static int findLength(int[] a, int[] b, int i, int j, IntRef maxRef, int[][] memo) {
        if (i == a.length) {
            return 0;
        }
        if (j == b.length) {
            return 0;
        }
        int val = 0;
        if (a[i] == b[j]) {
            int x = findLength(a, b, i + 1, j + 1, maxRef, memo) + 1;
            memo[i][j] = x;
            return x;
        }
        int x = findLength(a, b, i, j + 1, maxRef, memo);
//        int y = findLength(a, b, i + 1, j, maxRef, memo);
//        int max = Math.max(x, y);
        return x;
    }

    public static void main(String[] args) {
        Problem718 prob = new Problem718();
        System.out.println(prob.findLength(new int[]{1, 2, 3, 2, 1}, new int[]{3, 2, 1, 4, 7})); // 3
        System.out.println(prob.findLength(new int[]{1, 2, 3, 2, 1}, new int[]{1, 3, 2, 1, 4, 7})); // 3
        System.out.println(prob.findLength(new int[]{0, 1, 1, 1, 1}, new int[]{1, 0, 1, 0, 1})); // 2
        System.out.println(prob.findLength(new int[]{0, 0, 0, 0, 0, 0, 1, 0, 0, 0}, new int[]{0, 0, 0, 0, 0, 0, 0, 1, 0, 0})); // 9
    }
}

@@ -165,32 +165,57 @@
 B) %7B%0A        int
+ max = 0;%0A        Integer
 %5B%5D%5B%5D memo = new 
@@ -206,35 +206,39 @@
 %5B%5D%5B%5D memo = new 
-i
+I
 nt
+eger
 %5BA.length + 1%5D%5BB
@@ -263,49 +263,129 @@
     
-IntRef max = new IntRef();%0A        return
+for (int i = 0; i %3C A.length; i++) %7B%0A            for (int j = 0; j %3C B.length; j++) %7B%0A                max = Math.max(max,
  fin
@@ -402,24 +402,64 @@
  B, 
-0, 0, max
+i, j
 , memo)
+);%0A            %7D%0A        %7D%0A        return max
 ;%0A//
@@ -744,32 +744,48 @@
 + 1%5D + 1;%0A//    
+            %7D%0A//
                 
@@ -817,36 +817,16 @@
 i%5D%5Bj%5D);%0A
-//                %7D%0A
 //      
@@ -878,74 +878,8 @@
  %7D%0A%0A
-    private static class IntRef %7B%0A        private int val;%0A    %7D%0A%0A
     
@@ -947,23 +947,12 @@
  Int
-Ref maxRef, int
+eger
 %5B%5D%5B%5D
@@ -1098,11 +1098,11 @@
 int 
-val
+max
  = 0
@@ -1119,105 +1119,48 @@
 if (
-a%5Bi%5D == b%5Bj%5D) %7B%0A            int x = findLength(a, b, i + 1, j + 1, maxRef, memo) + 1;%0A           
+memo%5Bi%5D%5Bj%5D != null) %7B%0A            return
  mem
@@ -1170,12 +1170,8 @@
 %5D%5Bj%5D
- = x
 ;%0A  
@@ -1180,47 +1180,69 @@
     
-    return x;%0A        %7D%0A        int x =
+%7D%0A        if (a%5Bi%5D == b%5Bj%5D) %7B%0A            max = Math.max(max,
  fin
@@ -1256,16 +1256,20 @@
 (a, b, i
+ + 1
 , j + 1,
@@ -1274,116 +1274,53 @@
 1, m
-axRef, memo);%0A//        int y = findLength(a, b, i + 1, j, maxRef, memo);%0A//        int max = Math.max(x, y)
+emo) + 1);%0A        %7D%0A        memo%5Bi%5D%5Bj%5D = max
 ;%0A  
@@ -1332,16 +1332,18 @@
  return 
+ma
 x;%0A    %7D
@@ -1582,32 +1582,35 @@
 gth(new int%5B%5D%7B1,
+ 1,
  2, 3, 2, 1%7D, ne
@@ -1619,16 +1619,19 @@
 int%5B%5D%7B1,
+ 1,
  3, 2, 1

Update problem 718