# (力扣---动态规划)删除与获得点数

## (力扣—动态规划)删除与获得点数

nums的长度最大为20000。

#### python code

``````class Solution:
def deleteAndEarn(self, nums: List[int]) -> int:
if not nums: return 0
comp_nums = [i for i in range(1, max(nums) + 1)]
dicts = dict.fromkeys(comp_nums, 0)
for i in nums:
dicts[i] += 1
a = b = 0
keys = list(dicts.keys())
for i in range(len(keys)):
a, b = b, max(a + dicts[keys[i]] * keys[i], b)
return b
``````

#### c++ code

``````class Solution {
public:
int deleteAndEarn(vector<int>& nums) {
if(!nums.size()) return 0;
sort(nums.begin(), nums.end());                 //先排序
int maxF = nums[nums.size() - 1];               //获取最大值
map<int, int> comp;                             //利用map构造一个完整的“打家劫舍模型”
for(int i = 1; i <= maxF; i++)
comp[i] = 0;
for(int temp: nums)
comp[temp]++;
int a = 0;
int b = 0;
for(map<int, int>::iterator it = comp.begin(); it != comp.end(); it++){
int temp = b;
b = max(a + it->first * it->second, b);
a = temp;
}
return b;
}
};
``````

import this

``````The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
``````
原文作者：失恋味道的麻辣小龙虾
原文地址: https://blog.csdn.net/qq_40258761/article/details/88858611
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。