<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>

r'''
# Requirements

n = len(nums)
1 <= n <= 20,000
1 <= nums[i] <= 10,000

# Edge cases

1. Duplicates of a number
2. Numbers not in ascending order
3. n == 1

# Solutions

1. Brute force, simulation
    - O(2^n * n) time, O(n) auxiliary space
2. Count + sorting + memo / dp
    - O(nlogn + n) time, O(n) auxiliary space
2. Count + sorting + dp, space optimized
    - O(nlogn + n) time, O(1) auxiliary space
'''
def delete_and_earn_memo(nums: list[int]) -> int:
    # Prepare a sorted list of number and frequency pair
    freqs = Counter(nums)
    scores = list(freqs.items())
    scores.sort()

    # Memo
    m = len(scores)

    @lru_cache(maxsize = m + 2)
    def max_score(i: int) -> int:
        if i >= m:
            return 0

        num, freq = scores[i]
        if i + 1 < m and scores[i + 1][0] == num + 1:
            return max(num * freq + max_score(i + 2), max_score(i + 1))
        else:
            return num * freq + max_score(i + 1)

    return max_score(0)

def delete_and_earn_dp(nums: list[int]) -> int:
    # Prepare a sorted list of number and frequency pair
    freqs = Counter(nums)
    scores = list(freqs.items())
    scores.sort()

    # DP
    m = len(scores)
    dp = [0] * (m + 2)

    for i in range(m - 1, -1, -1):
        num, freq = scores[i]

        if i + 1 < m and scores[i + 1][0] == num + 1:
            dp[i] = max(num * freq + dp[i + 2], dp[i + 1])
        else:
            dp[i] = num * freq + dp[i + 1]

    return dp[0]

def delete_and_earn_dp_optimized(nums: list[int]) -> int:
    # Prepare a sorted list of number and frequency pair
    freqs = Counter(nums)
    scores = list(freqs.items())
    scores.sort()

    # DP
    m = len(scores)
    dp1, dp2 = 0, 0

    for i in range(m - 1, -1, -1):
        num, freq = scores[i]
        
        if i + 1 < m and scores[i + 1][0] == num + 1:
            dp0 = max(num * freq + dp2, dp1)
        else:
            dp0 = num * freq + dp1
        
        dp1, dp2 = dp0, dp1

    return dp1

class Solution:
    def deleteAndEarn(self, nums: List[int]) -> int:
        return delete_and_earn_memo(nums)
23838