-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeleteAndEarn.java
More file actions
46 lines (34 loc) · 896 Bytes
/
DeleteAndEarn.java
File metadata and controls
46 lines (34 loc) · 896 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
/**
* 740. 删除并获得点数
*/
public class DeleteAndEarn {
public int deleteAndEarn(int[] nums) {
int res = 0;
int maxVal = 0;
for(int t: nums){
maxVal = Math.max(maxVal, t);
}
int[] values = new int[maxVal+1];
int[] memo = new int[maxVal+1];
Arrays.fill(memo, -1);
for(int n: nums){
values[n] += n;
}
res = dp(maxVal, values, memo);
return res;
}
int dp(int start, int[] values, int[] memo){
if(start<0){
return 0;
}
if(memo[start]!=-1){
return memo[start];
}
memo[start] = Math.max(dp(start-1, values, memo),
dp(start-2, values, memo)+values[start]);
return memo[start];
}
}