美文网首页
740. Delete and Earn

740. Delete and Earn

作者: becauseyou_90cd | 来源:发表于2018-07-22 02:34 被阅读0次

https://leetcode.com/problems/delete-and-earn/description/

解题思路:

  1. 把数组nums中的所有值(相等的值相加)放到values中(从0到10000)
  2. 每一个值有两种情况,分别是可选(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);
}

}

相关文章

网友评论

      本文标题:740. Delete and Earn

      本文链接:https://www.haomeiwen.com/subject/dnakmftx.html