<p>You are given an integer array <code>nums</code>. You want to maximize the number of points you get by performing the following operation any number of times:</p>

<ul>
	<li>Pick any <code>nums[i]</code> and delete it to earn <code>nums[i]</code> points. Afterwards, you must delete <b>every</b> element equal to <code>nums[i] - 1</code> and <strong>every</strong> element equal to <code>nums[i] + 1</code>.</li>
</ul>

<p>Return <em>the <strong>maximum number of points</strong> you can earn by applying the above operation some number of times</em>.</p>

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

<pre>
<strong>Input:</strong> nums = [3,4,2]
<strong>Output:</strong> 6
<strong>Explanation:</strong> You can perform the following operations:
- Delete 4 to earn 4 points. Consequently, 3 is also deleted. nums = [2].
- Delete 2 to earn 2 points. nums = [].
You earn a total of 6 points.
</pre>

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

<pre>
<strong>Input:</strong> nums = [2,2,3,3,3,4]
<strong>Output:</strong> 9
<strong>Explanation:</strong> You can perform the following operations:
- Delete a 3 to earn 3 points. All 2&#39;s and 4&#39;s are also deleted. nums = [3,3].
- Delete a 3 again to earn 3 points. nums = [3].
- Delete a 3 once more to earn 3 points. nums = [].
You earn a total of 9 points.</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
</ul>

class Solution:
    
    max_points = {}

    def deleteAndEarn(self, nums: List[int]) -> int:
        
        # Sub Problems and optimal structures
        # Recurrence relation
        # Base case
        self.max_points = {}
        return self.deleteAndEarnRecursiveV2(nums)
    

    def deleteAndEarnRecursiveV2(self, nums):
        if len(nums) == 0:
            return 0
        if len(nums) == 1:
            return nums[0]
        
        points = defaultdict(int)
        max_number = nums[0]
        for num in nums:
            points[num] += num
            max_number = max(max_number, num)
        self.points = points
        
        return self.maxPoints(max_number)
    
    @cache
    def maxPoints(self, num):
        
        if num == 0:
            return 0
        if num == 1:
            return self.points[1]
        
        picked = self.maxPoints(num - 2) + self.points[num]
        skipped = self.maxPoints(num-1)
        
        return max(picked, skipped)
        

    

        


   

    def deleteAndEarnRecursive(self, i, nums):
        
        # print(f"Index:{i}  Nums:{nums}")
        # base
        if i < 0 or len(nums) <= 0:
            return 0
        
        if len(nums) == 1:
            return nums[0]
        
        nums.sort()
        suffix = '.'.join([str(x) for x in nums])
        key = f"{i}_{suffix}"
        
        if key in self.max_points:
            return self.max_points[key]

        # delete i
        picked = nums[i]
        new_list = []
        for j, num in enumerate(nums):
            if num != picked - 1 and num != picked + 1 and j != i:
                new_list.append(num)
        # print(f"Picked: {picked}   NL: {new_list}")
        picked_points = self.deleteAndEarnRecursive(len(new_list) - 1, new_list) + picked
        
            
        # do not delete i
        skipped_points = self.deleteAndEarnRecursive(i-1, nums) 

        # print(f"PP:{picked_points}  SP:{skipped_points}")
        
        result =  max(picked_points, skipped_points)
        self.max_points[key] = result

        return self.max_points[key]
        
        
24731