题解 | #删除相邻数字的最大分数#
删除相邻数字的最大分数
http://www.nowcoder.com/practice/3bcf72c738b6494bbe1ebe0ffde56152
将数组进行转化 nums[i] => trans[nums[i]] += nums[i] 数组下标对应选择的总和 转化为dp[i] = Math.max(dp[i - 1], dp[i - 2] + trans[i])的问题
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
int[] p = new int[n];
for (int i = 0; i < n; i++) {
p[i] = reader.nextInt();
}
int[] dp = new int[10001];
int[] trans = new int[10001];
for (int i = 0; i < p.length; i++) {
trans[p[i]] += p[i];
}
dp[0] = 0;
dp[1] = trans[1];
for (int i = 2; i < trans.length; i++) {
dp[i] = Math.max(dp[i - 1], dp[i - 2] + trans[i]);
}
System.out.println(dp[dp.length - 1]);
}
}