package leetcode;

/**
 * https://leetcode.com/problems/uncrossed-lines/
 */
public class Problem1035 {
    public int maxUncrossedLines(int[] A, int[] B) {
        int answer = 0;
        for (int i = 0; i < A.length; i++) {
            answer = Math.max(answer, maxUncrossedLines(A, B, i, 0));
        }
        return answer;
    }

    private static int maxUncrossedLines(int[] a, int[] b, int i, int j) {
        if (i == a.length || j == b.length) {
            return 0;
        }
        int max = 0;
        if (a[i] == b[j]) {
            max = Math.max(max, maxUncrossedLines(a, b, i + 1, j + 1) + 1);
        }
        max = Math.max(max, maxUncrossedLines(a, b, i, j + 1));
        return max;
    }

    public static void main(String[] args) {
        Problem1035 prob = new Problem1035();
        System.out.println(prob.maxUncrossedLines(new int[]{1, 4, 2}, new int[]{1, 2, 4})); // 2
        System.out.println(prob.maxUncrossedLines(new int[]{2, 5, 1, 2, 5}, new int[]{10, 5, 2, 1, 5, 2})); // 3
        System.out.println(prob.maxUncrossedLines(new int[]{2, 5, 1, 2, 5, 8}, new int[]{10, 5, 2, 1, 5, 2, 8})); // 4
        System.out.println(prob.maxUncrossedLines(new int[]{1, 3, 7, 1, 7, 5}, new int[]{1, 9, 2, 5, 1})); // 2
    }
}

@@ -162,106 +162,74 @@
     
-int answer = 0;%0A        for (int i = 0; i %3C A.length; i++) %7B%0A            answer = Math.max(answer,
+Integer%5B%5D%5B%5D memo = new Integer%5BA.length%5D%5BB.length%5D;%0A        return
  max
@@ -253,47 +253,19 @@
  B, 
-i, 0));%0A        %7D%0A        return answer
+0, 0, memo)
 ;%0A  
@@ -340,16 +340,34 @@
 i, int j
+, Integer%5B%5D%5B%5D memo
 ) %7B%0A    
@@ -453,122 +453,62 @@
    i
-nt max = 0;%0A        if (a%5Bi%5D == b%5Bj%5D) %7B%0A            max = Math.max(max, maxUncrossedLines(a, b, i + 1, j + 1) + 1)
+f (memo%5Bi%5D%5Bj%5D != null) %7B%0A            return memo%5Bi%5D%5Bj%5D
 ;%0A  
@@ -527,214 +527,134 @@
     
+int 
 max = 
-Math.max(max, maxUncrossedLines(a, b, i, j + 1));%0A        return max;
+0;%0A        for (int x = i; x %3C a.length; x++) %7B
 %0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem1035 prob = new Problem1035();%0A        System.out.println(prob.
+    if (a%5Bx%5D == b%5Bj%5D) %7B%0A                max = Math.max(max, 
 maxU
@@ -671,200 +671,86 @@
 nes(
-new int%5B%5D%7B1, 4, 2%7D, new int%5B%5D%7B1, 2, 4%7D)); // 2%0A        System.out.println(prob.maxUncrossedLines(new int%5B%5D%7B2, 5, 1, 2, 5%7D, new int%5B%5D%7B10, 5, 2, 1, 5, 2%7D)); // 3%0A        System.out.println(prob.
+a, b, x + 1, j + 1, memo) + 1);%0A            %7D%0A            max = Math.max(max, 
 maxU
@@ -767,188 +767,87 @@
 nes(
-new int%5B%5D%7B2, 5, 1, 2, 5, 8%7D, new int%5B%5D%7B10, 5, 2, 1, 5, 2, 8%7D)); // 4%0A        System.out.println(prob.maxUncrossedLines(new int%5B%5D%7B1, 3, 7, 1, 7, 5%7D, new int%5B%5D%7B1, 9, 2, 5, 1%7D)); // 2
+a, b, x, j + 1, memo));%0A        %7D%0A        memo%5Bi%5D%5Bj%5D = max;%0A        return max;
 %0A   

Solve problem 1035