Untitled

mail@pastecode.io avatar
unknown
plain_text
8 months ago
631 B
0
Indexable
Never
class Solution {
public:
    vector<int>dp;
    unordered_map<int,int>freq;
    int solve(int i,vector<int>&v){
        if(i>=v.size()) return 0;
        if(dp[i]!=-1) return dp[i];

        int inc;
        if(i+1<v.size() && abs(v[i+1]-v[i])==1) inc=solve(i+2,v);
        else inc=solve(i+1,v);

        return dp[i]=max(freq[v[i]]*v[i]+inc, solve(i+1,v));//it has to be done for all numbers

    }
    int deleteAndEarn(vector<int>& nums) {
        set<int>s;
        for(auto i:nums) {s.insert(i);freq[i]++;}
        vector<int>v(s.begin(),s.end());

        dp.resize(v.size(),-1);
        return solve(0,v);
        
    }
};
Leave a Comment