class Solution {
public:
int deleteAndEarn(vector<int>& nums) {
int maxVal = 0;
for (int val : nums) {
maxVal = std::max(maxVal, val);
}
std::vector<int> sum(maxVal + 1);
for (int val : nums) {
sum[val] += val;
}
return rob(sum);
}
private:
int rob(std::vector<int>& nums) {
int size = nums.size();
if (size == 0) {
return 0;
}
if (size == 1) {
return nums[0];
}
int dp0 = nums[0];
int dp1 = std::max(dp0, nums[1]);
for (int i = 2; i < size; ++i) {
int tmp = dp1;
dp1 = std::max(dp0 + nums[i], dp1);
dp0 = tmp;
}
return dp1;
}
};
——————————————————————