https://leetcode.com/problems/delete-and-earn/description/
解题思路:
- 把数组nums中的所有值(相等的值相加)放到values中(从0到10000)
- 每一个值有两种情况,分别是可选(take)和不可选(skip)
3.返回take和skip中最大的那个
代码如下:
class Solution {
public int deleteAndEarn(int[] nums) {
int n = 10001;
int[] values = new int[n];
for(int num : nums)
values[num] += num;
int take = 0; int skip = 0;
for (int i = 1; i < n; i++){
int oldTake = take;
take = skip + values[i];
skip = Math.max(skip, oldTake);
}
return Math.max(skip, take);
}
}
网友评论