LeetCode740(2021.5.5)

LC740. 删除并获得点数

class Solution {
public:
    int rob(vector<int> &nums) {
        int first = nums[0], second = max(nums[0], nums[1]);
        for (int i = 2; i < nums.size(); i++) {
            int temp = second;
            second = max(first + nums[i], second);
            first = temp;
        }
        return second;
    }

    int deleteAndEarn(vector<int> &nums) {
        vector<int> sum(10010);
        for (int val : nums) {
            sum[val] += val;
        }
        return rob(sum);
    }
};

发表评论