<p>Given two integer arrays <code>nums1</code> and <code>nums2</code>, return <em>the maximum length of a subarray that appears in <strong>both</strong> arrays</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums1 = [1,2,3,2,1], nums2 = [3,2,1,4,7]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The repeated subarray with maximum length is [3,2,1].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums1 = [0,0,0,0,0], nums2 = [0,0,0,0,0]
<strong>Output:</strong> 5
<strong>Explanation:</strong> The repeated subarray with maximum length is [0,0,0,0,0].
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums1.length, nums2.length &lt;= 1000</code></li>
	<li><code>0 &lt;= nums1[i], nums2[i] &lt;= 100</code></li>
</ul>

class Solution:
    def dp(self, i: int, j: int, nums1: List[int], nums2: List[int]) -> int:
        if (i,j) in memo:
            return memo[(i,j)]
        elif i == j:
            locs = {k for k in range(len(nums2)) if nums2[k] == nums1[i]}
            lgth = int(len(locs) > 0)
            memo[(i,j)] = (locs, lgth)
            return (locs, lgth)
        prev_locs, lgth = self.dp(i+1, j, nums1, nums2)
        if lgth == 0:
            locs = {k for k in range(len(nums2)) if nums2[k] == nums1[i]}
            lgth = int(len(locs) > 0)
            memo[(i,j)] = (locs, lgth)
            return (locs, lgth)
        locs = set()
        for idx in prev_locs:
            if idx-1 >= 0 and nums2[idx-1:idx+lgth] == nums1[i:i+1+lgth]:
                locs.add(idx-1)
        if len(locs) > 0:
            lgth += 1
        else:
            locs = prev_locs
            locs = locs.union({k for k in range(len(nums2)+1-lgth) if nums2[k:k+lgth] == nums1[i:i+lgth]})
        memo[(i,j)] = (locs, lgth)
        return (locs, lgth)

    def findLength(self, nums1: List[int], nums2: List[int]) -> int:
        global memo
        memo = {}
        return self.dp(0, len(nums1)-1, nums1, nums2)[1]
49800