https://leetcode.com/problems/delete-and-earn/description/
int deleteAndEarn(vector<int>& nums) {
vector<int> ns(100001, 0);
for(int i: nums) ns[i]++;
int skip = 0, keep = 0;
for(int i=1; i<ns.size(); i++) {
int keepi = skip + i * ns[i];
skip = max(keep, skip);
keep = keepi;
}
return max(keep, skip);
}
No comments:
Post a Comment