<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 {
public:
    // int solve(int i,int n,vector<int>&v,vector<int>&dp){
    //     if(i>=n) return 0;
    //     if(dp[i]!=-1) return dp[i];
    //     int t=0,nt=0;
    //     t=v[i]+solve(i+2,n,v,dp);
    //     nt=solve(i+1,n,v,dp);
    //     return dp[i] = max(t,nt);
    // }

    // int deleteAndEarn(vector<int>& nums) {
    //     int maxi=*max_element(nums.begin(),nums.end());
    //     vector<int>v(maxi+1,0);
    //     vector<int>dp(maxi+1,0);
    //     for(auto i:nums) v[i]+=i;
    //     // return solve(0,maxi+1,v,dp);
    //     dp[maxi]=v[maxi];
    //     dp[maxi-1]=v[maxi-1];

    //     if(maxi==1) return v[maxi];

    //     for(int i=maxi-2;i>=0;i--){
    //         int t=v[i]+dp[i+2];
    //         int nt=max(dp[i+1],dp[i+2]);
    //         dp[i]=max(t,nt);
    //     }

    //     return dp[0];
    // }

    int deleteAndEarn(vector<int>& nums) {
    if (nums.empty()) return 0;
    
    int maxi = *max_element(nums.begin(), nums.end());
    vector<int> v(maxi + 1, 0);
    vector<int> dp(maxi + 1, 0);

    // Create the 'v' array where v[i] holds the total points we can earn by deleting 'i'.
    for (int i : nums) {
        v[i] += i;
    }

    if(maxi==1) return v[maxi];

    // Base cases for dp
    dp[maxi] = v[maxi];
    if (maxi > 1) dp[maxi-1] = max(v[maxi-1], dp[maxi]);

    // Bottom-up dynamic programming approach
    for (int i = maxi - 2; i >= 0; i--) {
        dp[i] = max(v[i] + dp[i + 2], dp[i + 1]);
    }

    return dp[0];
}

};
14456