Clean cpp code


  • 0
    H
        int deleteAndEarn(vector<int>& nums) {
            vector<int> dp(10001, 0);
            sort(nums.begin(), nums.end());
            dp[1] = upper_bound(nums.begin(), nums.end(), 1) - lower_bound(nums.begin(), nums.end(),1);
            int tmp2= upper_bound(nums.begin(), nums.end(), 2) - lower_bound(nums.begin(), nums.end(),2);
            dp[2] = max( tmp2*2, dp[1] );
            int tmpi=0;
            for(int i=2; i< 10001; i++){
                tmpi = upper_bound(nums.begin(), nums.end(), i) - lower_bound(nums.begin(), nums.end(),i);
                dp[i] = max(dp[i-2]+ tmpi*i, dp[i-1]);
            }
            return dp[10000];
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.