# LWC 61：740. Delete and Earn

## LWC 61：740. Delete and Earn

Problem:

Given an array nums of integers, you can perform operations on the array. In each operation, you pick any nums[i] and delete it to earn nums[i] points. After, you must delete every element equal to nums[i] - 1 or nums[i] + 1. You start with 0 points. Return the maximum number of points you can earn by applying such operations.

Example 1:

Input: nums = [3, 4, 2] Output: 6 Explanation: Delete 4 to earn 4 points, consequently 3 is also deleted. Then, delete 2 to earn 2 points. 6 total points are earned.

Example 2:

Input: nums = [2, 2, 3, 3, 3, 4] Output: 9 Explanation: Delete 3 to earn 3 points, deleting both 2’s and the 4. Then, delete 3 again to earn 3 points, and 3 again to earn 3 points. 9 total points are earned.

Note:

The length of nums is at most 20000.

Each element nums[i] is an integer in the range [1, 10000].

Java版本：（递归+记忆化）

```    public int deleteAndEarn(int[] nums) {
Map<Integer, Integer> mem = new HashMap<>();
for (int n : nums) {
mem.put(n, mem.getOrDefault(n, 0) + 1);
}

int[] cnt = new int[mem.size()];
Set<Integer> keys = mem.keySet();

int[] key = keys.stream().mapToInt(i -> i).toArray();
Arrays.sort(key);

for (int i = 0; i < key.length; ++i) {
cnt[i] = mem.get(key[i]);
}
dp = new int[20000 + 16];
return robot(key, cnt, 0);
}

int[] dp;
public int robot(int[] key, int[] cnt, int pos) {
if (pos >= key.length) return 0;
if (dp[pos] > 0) return dp[pos];

int ans = 0;
int choose = 0;

// choose
if (pos + 1 < key.length && key[pos + 1] - 1 == key[pos]) {
choose = key[pos] * cnt[pos] + robot(key, cnt, pos + 2);
}
else {
choose = key[pos] * cnt[pos] + robot(key, cnt, pos + 1);
}
ans = Math.max(ans, choose);
// no choose
ans = Math.max(ans, robot(key, cnt, pos + 1));
return dp[pos] = ans;
}```

```    public int deleteAndEarn(int[] nums) {
if (nums.length == 0) return 0;

Map<Integer, Integer> mem = new HashMap<>();
for (int n : nums) {
mem.put(n, mem.getOrDefault(n, 0) + 1);
}

int[] cnt = new int[mem.size()];
Set<Integer> keys = mem.keySet();

int[] key = keys.stream().mapToInt(i -> i).toArray();
Arrays.sort(key);

for (int i = 0; i < key.length; ++i) {
cnt[i] = mem.get(key[i]);
}

int[] dp = new int[20000 + 16];
int last = key.length;

dp[last - 1] = key[last - 1] * cnt[last - 1];
for (int i = last - 2; i >= 0; --i) {
if (key[i] + 1 == key[i + 1]) {
dp[i] = Math.max(dp[i], dp[i + 2] + key[i] * cnt[i]);
}
else {
dp[i] = Math.max(dp[i], dp[i + 1] + key[i] * cnt[i]);
}

dp[i] = Math.max(dp[i], dp[i + 1]);
}

return dp[0];
}```

```      public int deleteAndEarn(int[] nums) {
int[] cnt = new int[10016];
for (int num : nums) cnt[num]++;

int[] dp = new int[10016];
dp[0] = 0;
dp[1] = cnt[1];

for (int i = 2; i <= 10000; ++i) {
dp[i] = Math.max(dp[i - 1], dp[i - 2] + cnt[i] * i);
}

return dp[10000];
}```

Python版本：

```    def deleteAndEarn(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
cnt = [0] * 10016
for num in nums:
cnt[num] = cnt[num] + 1
dp = [0] * 10016
dp[0] = 0
dp[1] = cnt[1]

for i in range(2, 10001):
dp[i] = max(dp[i - 1], dp[i - 2] + i * cnt[i])

return dp[10000]```

0 条评论

## 相关文章

### concrrent类下 BlockingDeque 下 自己实现代码编写

java6增加了两种容器类型，Deque和BlockingDeque,它们分别对Queue和BlockingQueue进行了扩展。 Deque是一个双端队...

732

### LWC 52：686. Repeated String Match

LWC 52：686. Repeated String Match 传送门：686. Repeated String Match Problem: Given...

1965

### Java中处理正则表达式的工具类——总有一个适合你

import java.util.ArrayList; import java.util.List; import java.util.regex.Match...

2685

771

### 详解Java动态代理机制（二）----cglib实现动态代理

上篇文章的结尾我们介绍了普通的jdk实现动态代理的主要不足在于：它只能代理实现了接口的类，如果一个类没有继承于任何的接口，那么就不能代理该类，原因是我...

24010

2905

36011

36811

1885

1789