leetcode - 740. Delete and Earn

先计数,再动态规划
在这里插入图片描述

class Solution {
public:
    int deleteAndEarn(vector<int>& nums) {
        
        fill(map,map+10000,0);
        for(vector<int>::iterator it=nums.begin();it!=nums.end();++it)
        {
            ++map[*it];
        }
        int c1(0),c2=map[1],c3=map[1];
        bool f(1);
        for(int i=2;i<10001;++i)
        {
            if(map[i])
            {
                c3=max(c1+map[i]*i,c2);
                c1=c2;
                c2=c3;
                f=1;
            }else if(f)
            {
                c1=c2;
                f=0;
            }
        }
        return c3;
    }
private:
    short map[10001];
};

猜你喜欢

转载自blog.csdn.net/weixin_41938758/article/details/88724795