@@ -0,0 +1,1145 @@
+// Dynamic programming solution to the problem.%0A// Time complexity is O(A * B).%0A// Space complexity is O(A * B) for dp%5B%5D%5B%5D array;%0A//%0A// I found it a bit counter-intuitive to use dynamic programming this way, especially when%0A// updating %60len%60 variable as you compute dp values. However, it kind of makes sense once you print%0A// dp array - each sequence will stop at certain %5Bi, j%5D combination, and the transitions only happen%0A// for matching pairs, otherwise you start with 0. And while this is happening, you keep track of%0A// maximum length of a subarray.%0Aclass Solution %7B%0A  public int findLength(int%5B%5D A, int%5B%5D B) %7B%0A    if (A.length == 0 %7C%7C B.length == 0) return 0;%0A%0A    int%5B%5D%5B%5D dp = new int%5BA.length%5D%5BB.length%5D;%0A%0A    for (int i = 0; i %3C A.length; i++) %7B%0A      dp%5Bi%5D%5B0%5D = A%5Bi%5D == B%5B0%5D ? 1 : 0;%0A    %7D%0A%0A    for (int j = 0; j %3C B.length; j++) %7B%0A      dp%5B0%5D%5Bj%5D = A%5B0%5D == B%5Bj%5D ? 1 : 0;%0A    %7D%0A%0A    int len = 0;%0A    for (int i = 1; i %3C A.length; i++) %7B%0A      for (int j = 1; j %3C B.length; j++) %7B%0A        if (A%5Bi%5D == B%5Bj%5D) %7B%0A          dp%5Bi%5D%5Bj%5D = dp%5Bi-1%5D%5Bj-1%5D + 1;%0A          len = Math.max(len, dp%5Bi%5D%5Bj%5D);%0A        %7D%0A      %7D%0A    %7D%0A%0A    return len;%0A  %7D%0A%7D%0A

add solution