Medium
思路参考House Rubbery和Gas station,之前我纠结重复的数字怎么处理,结果很简单。维持一个数组,以num为index,值为该num拥有的总points数。多次遇到就多次加就好了。
class Solution {
public int deleteAndEarn(int[] nums) {
int[] values = new int[10000];
for (int num : nums){
values[num] += num;
}
int take = 0;
int skip = 0;
for (int i = 0; i < values.length; i++){
int takeCurt = skip + values[i];
skip = Math.max(take, skip);
take = takeCurt;
}
return Math.max(take, skip);
}
}