0740.cpp (433B)
1 class Solution { 2 public: 3 int deleteAndEarn(vector<int> &nums) { 4 int n = *max_element(nums.begin(), nums.end()) + 1; 5 vector<int> count(n); 6 for (int n : nums) 7 count[n] += n; 8 9 int prev1 = 0, prev2 = 0; 10 for (int i = 0; i < n; i++) { 11 int tmp = prev1; 12 prev1 = max(prev2 + count[i], prev1); 13 prev2 = tmp; 14 } 15 16 return prev1; 17 } 18 };