740_DeleteandEarn - a920604a/leetcode GitHub Wiki


title: 740. Delete and Earn tags:
- dp categories: leetcode comments: false

solution

這就是變形的強盜問題198. House Robber

class Solution {
public:
    int deleteAndEarn(vector<int>& nums) {
        //  
        vector<int> dp(10001,0);
        for(int n:nums) dp[n]++;
        for(int i=1;i<=10000;++i) dp[i] = i*dp[i];
        int ret = 0;
        ret = max(dp[0], dp[1]);
        for(int i=2;i<=10000;++i){
            dp[i] = max(dp[i-2]+dp[i], dp[i-1]);
            ret = max(ret, dp[i]);
        }
        return ret;
        
    }
};

analysis

  • time complexity O(n)
  • space complexity O(1)
⚠️ **GitHub.com Fallback** ⚠️