package leetcode;

/**
 * https://leetcode.com/problems/maximum-length-of-repeated-subarray/
 */
public class Problem718 {
    public int findLength(int[] A, int[] B) {
        int max = 0;
        Integer[][] memo = new Integer[A.length + 1][B.length + 1];
        for (int i = 0; i < A.length; i++) {
            for (int j = 0; j < B.length; j++) {
                max = Math.max(max, findLength(A, B, i, j, memo));
            }
        }
        return max;
//        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 int findLength(int[] a, int[] b, int i, int j, Integer[][] memo) {
        if (i == a.length) {
            return 0;
        }
        if (j == b.length) {
            return 0;
        }
        int max = 0;
        if (memo[i][j] != null) {
            return memo[i][j];
        }
        if (a[i] == b[j]) {
            max = Math.max(max, findLength(a, b, i + 1, j + 1, memo) + 1);
        }
        memo[i][j] = max;
        return max;
    }

    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, 1, 2, 3, 2, 1}, new int[]{1, 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
    }
}

@@ -187,306 +187,8 @@
  0;%0A
-        Integer%5B%5D%5B%5D memo = new Integer%5BA.length + 1%5D%5BB.length + 1%5D;%0A        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, findLength(A, B, i, j, memo));%0A            %7D%0A        %7D%0A        return max;%0A//        int max = 0;%0A//
     
@@ -243,18 +243,16 @@
 h + 1%5D;%0A
-//
         
@@ -289,26 +289,24 @@
 = 0; i--) %7B%0A
-//
             
@@ -343,26 +343,24 @@
 = 0; j--) %7B%0A
-//
             
@@ -383,18 +383,16 @@
 B%5Bj%5D) %7B%0A
-//
         
@@ -440,18 +440,16 @@
 1%5D + 1;%0A
-//
         
@@ -450,34 +450,32 @@
               %7D%0A
-//
                 
@@ -507,18 +507,16 @@
 i%5D%5Bj%5D);%0A
-//
         
@@ -513,34 +513,32 @@
 ;%0A            %7D%0A
-//
         %7D%0A//    
@@ -531,18 +531,16 @@
       %7D%0A
-//
         
@@ -560,1020 +560,7 @@
    %7D
-%0A%0A    private static int findLength(int%5B%5D a, int%5B%5D b, int i, int j, Integer%5B%5D%5B%5D memo) %7B%0A        if (i == a.length) %7B%0A            return 0;%0A        %7D%0A        if (j == b.length) %7B%0A            return 0;%0A        %7D%0A        int max = 0;%0A        if (memo%5Bi%5D%5Bj%5D != null) %7B%0A            return memo%5Bi%5D%5Bj%5D;%0A        %7D%0A        if (a%5Bi%5D == b%5Bj%5D) %7B%0A            max = Math.max(max, findLength(a, b, i + 1, j + 1, memo) + 1);%0A        %7D%0A        memo%5Bi%5D%5Bj%5D = max;%0A        return max;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem718 prob = new Problem718();%0A        System.out.println(prob.findLength(new int%5B%5D%7B1, 2, 3, 2, 1%7D, new int%5B%5D%7B3, 2, 1, 4, 7%7D)); // 3%0A        System.out.println(prob.findLength(new int%5B%5D%7B1, 1, 2, 3, 2, 1%7D, new int%5B%5D%7B1, 1, 3, 2, 1, 4, 7%7D)); // 3%0A        System.out.println(prob.findLength(new int%5B%5D%7B0, 1, 1, 1, 1%7D, new int%5B%5D%7B1, 0, 1, 0, 1%7D)); // 2%0A        System.out.println(prob.findLength(new int%5B%5D%7B0, 0, 0, 0, 0, 0, 1, 0, 0, 0%7D, new int%5B%5D%7B0, 0, 0, 0, 0, 0, 0, 1, 0, 0%7D)); // 9%0A    %7D
 %0A%7D%0A

Solve problem 718