您的位置:首页 > 其它

740. Delete and Earn

2017-12-14 20:30 295 查看

740. Delete and Earn

题目描述: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.


题目大意:给定一个int数组,每次操作可以删除num[i]并且获得nums[i]的分数,之后必须删除
nums[i] - 1
o或者
nums[i] + 1
,求能获得最大的分数。

思路:DP

代码

package DP;

/**
* @Author OovEver
* @Date 2017/12/14 20:22
*/
public class LeetCode740 {
public int deleteAndEarn(int[] nums) {
int[] count = new int[10001];
for (int n : nums) {
count
+= n;
}
int[] dp = new int[10003];
for(int i=10000;i>=0;i--) {
dp[i] = Math.max(count[i] + dp[i + 2], dp[i + 1]);
}
return dp[0];
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: