740. Delete and Earn

2018-07-22  本文已影响0人  becauseyou_90cd

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);
}

}

上一篇 下一篇

猜你喜欢

热点阅读