代码:
class Solution {
public int deleteAndEarn(int[] nums) {
int[][] res = new int[nums.length][2];
Arrays.sort(nums);
res[0][0] = 0;
res[0][1] = nums[0];
for(int i=1;i<nums.length;i++){
if(nums[i]==nums[i-1]){
res[i][0] = res[i-1][0];
res[i][1] = res[i-1][1] + nums[i];
}else if(nums[i]==nums[i-1]+1){
res[i][0] = Math.max(res[i-1][0],res[i-1][1]);
res[i][1] = res[i-1][0] + nums[i];
}else{
res[i][0] = Math.max(res[i-1][0],res[i-1][1]);
res[i][1] = Math.max(res[i-1][0] + nums[i],res[i-1][1] + nums[i]);
}
}
return Math.max(res[nums.length-1][0],res[nums.length-1][1]);
}
}