2024/12/7 灵茶题单 dp
This commit is contained in:
parent
24cc5ae131
commit
26b0c8c863
|
@ -0,0 +1,21 @@
|
|||
package com.cool.ling_cha_mount.dp;
|
||||
|
||||
public class Num740 {
|
||||
public int deleteAndEarn(int[] nums) {
|
||||
int max = Integer.MIN_VALUE;
|
||||
for (int num : nums) {
|
||||
max = Math.max(max, num);
|
||||
}
|
||||
int[] dp = new int[max + 1];
|
||||
for (int num : nums) {
|
||||
dp[num] += num;
|
||||
}
|
||||
int prevMax = 0, currMax = dp[0];
|
||||
for (int i = 1; i < dp.length; i++) {
|
||||
int temp = currMax;
|
||||
currMax = Math.max(currMax, prevMax + dp[i]);
|
||||
prevMax = temp;
|
||||
}
|
||||
return currMax;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue