<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code>. We write the integers of <code>nums1</code> and <code>nums2</code> (in the order they are given) on two separate horizontal lines.</p>

<p>We may draw connecting lines: a straight line connecting two numbers <code>nums1[i]</code> and <code>nums2[j]</code> such that:</p>

<ul>
	<li><code>nums1[i] == nums2[j]</code>, and</li>
	<li>the line we draw does not intersect any other connecting (non-horizontal) line.</li>
</ul>

<p>Note that a connecting line cannot intersect even at the endpoints (i.e., each number can only belong to one connecting line).</p>

<p>Return <em>the maximum number of connecting lines we can draw in this way</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/04/26/142.png" style="width: 400px; height: 286px;" />
<pre>
<strong>Input:</strong> nums1 = [1,4,2], nums2 = [1,2,4]
<strong>Output:</strong> 2
<strong>Explanation:</strong> We can draw 2 uncrossed lines as in the diagram.
We cannot draw 3 uncrossed lines, because the line from nums1[1] = 4 to nums2[2] = 4 will intersect the line from nums1[2]=2 to nums2[1]=2.
</pre>

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

<pre>
<strong>Input:</strong> nums1 = [2,5,1,2,5], nums2 = [10,5,2,1,5,2]
<strong>Output:</strong> 3
</pre>

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

<pre>
<strong>Input:</strong> nums1 = [1,3,7,1,7,5], nums2 = [1,9,2,5,1]
<strong>Output:</strong> 2
</pre>

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

<ul>
	<li><code>1 &lt;= nums1.length, nums2.length &lt;= 500</code></li>
	<li><code>1 &lt;= nums1[i], nums2[j] &lt;= 2000</code></li>
</ul>

class Solution:
    def maxUncrossedLines(self, nums1: List[int], nums2: List[int]) -> int:
        # d={}
        # for i in range(len(nums2)):
        #     if nums2[i] not in d:
        #         d[nums2[i]]=[i]
        #     else:
        #         d[nums2[i]].append(i)
        # cache={}
        # def comb(i,latest):
        #     if i==len(nums1):
        #         return 0
        #     if (i,latest) in cache:
        #         return cache[(i,latest)]

        #     val=nums1[i]
        #     res=0
        #     if val not in d:
        #         return comb(i+1,latest)
        #     pick=notpick=0
        #     for eq in d[val]:
        #         if eq>latest:
        #             pick=max(pick,1+comb(i+1,eq))
        #     notpick=comb(i+1,latest)
        #     cache[(i,latest)]=max(pick,notpick)
        #     return cache[(i,latest)]
        # return comb(0,-1)
        
        d = {}
        for i in range(len(nums2)):
            if nums2[i] not in d:
                d[nums2[i]] = [i]
            else:
                d[nums2[i]].append(i)
        cache = [[-1] * (len(nums2) + 1) for _ in range(len(nums1))]

        def comb(i, latest):
            if i == len(nums1):
                return 0
            if cache[i][latest + 1] != -1:
                return cache[i][latest + 1]

            val = nums1[i]
            pick = 0
            if val in d:
                for eq in d[val]:
                    if eq > latest:
                        pick = max(pick, 1 + comb(i + 1, eq))
            notpick = comb(i + 1, latest)
            cache[i][latest + 1] = max(pick, notpick)
            return cache[i][latest + 1]

        return comb(0, -1)

18700