<p>Given an array <code>nums</code> of integers, return <em>the length of the longest arithmetic subsequence in</em> <code>nums</code>.</p>

<p><strong>Note</strong> that:</p>

<ul>
	<li>A <strong>subsequence</strong> is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.</li>
	<li>A sequence <code>seq</code> is arithmetic if <code>seq[i + 1] - seq[i]</code> are all the same value (for <code>0 &lt;= i &lt; seq.length - 1</code>).</li>
</ul>

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

<pre>
<strong>Input:</strong> nums = [3,6,9,12]
<strong>Output:</strong> 4
<strong>Explanation: </strong> The whole array is an arithmetic sequence with steps of length = 3.
</pre>

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

<pre>
<strong>Input:</strong> nums = [9,4,7,2,10]
<strong>Output:</strong> 3
<strong>Explanation: </strong> The longest arithmetic subsequence is [4,7,10].
</pre>

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

<pre>
<strong>Input:</strong> nums = [20,1,15,3,10,5,8]
<strong>Output:</strong> 4
<strong>Explanation: </strong> The longest arithmetic subsequence is [20,15,10,5].
</pre>

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

<ul>
	<li><code>2 &lt;= nums.length &lt;= 1000</code></li>
	<li><code>0 &lt;= nums[i] &lt;= 500</code></li>
</ul>

class Solution:
    def longestArithSeqLength(self, nums: List[int]) -> int:
        minDist = -500
        maxDist = 500

        cache = dict()
        maxSubsecLen = 1

        for i in reversed(range(len(nums))):
            curEl = i
            j = i+1
            iMaxSubsecLen = 1
            while j < len(nums):
                d = nums[j] - nums[i]
                if (j, d) in cache:
                    subsecLen = cache[(j, d)] + 1                
                else:
                    subsecLen = 2
                if (i,d) in cache:
                    cache[(i,d)] = max(cache[(i,d)], subsecLen)
                else:
                    cache[(i,d)] = subsecLen
                iMaxSubsecLen = max(iMaxSubsecLen, subsecLen)
                j += 1
                
            maxSubsecLen = max(maxSubsecLen, iMaxSubsecLen)

 
        # for i in reversed(range(len(nums))):
        #     d = minDist
        #     while d <= maxDist:                    
        #         curEl = i
        #         j = i+1
        #         subsecLen = 1
        #         while j < len(nums):
        #             if nums[j] - nums[curEl] == d:
        #                 if (j, d) in cache:
        #                     subsecLen += cache[(j, d)]
        #                     break
        #                 subsecLen += 1
        #                 curEl = j
        #             j += 1
                
        #         cache[(i, d)] = subsecLen
        #         maxSubsecLen = max(maxSubsecLen, subsecLen)
        #         d += 1
        
        return maxSubsecLen

        
59633