2021-05-05 LeetCode每日一题
链接:https://leetcode-cn.com/problems/delete-and-earn/
此问题可以转换为https://leetcode-cn.com/problems/house-robber/ 。
计算每个数的总点数,得到一个新数组,然后就是跟https://leetcode-cn.com/problems/house-robber/ 一样的解法了。
关于https://leetcode-cn.com/problems/house-robber/问题的解法,可以看该文章https://blog.csdn.net/a_helloword/article/details/116430698
class Solution {
public int deleteAndEarn(int[] nums) {
int[] values = new int[10000];
for (int num : nums) {
values[num] += num;
}
int cur = 0, prev = 0, temp = 0;
for (int num : values) {
temp = cur;
cur = Math.max(cur, prev + num);
prev = temp;
}
return cur;
}
}