740. Delete and Earn

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);
    }
}
    原文作者:greatfulltime
    原文地址: https://www.jianshu.com/p/ae32a44c9e67
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞