740. Delete and Earn
2017-12-14 20:30
295 查看
740. Delete and Earn
题目描述:Given an arraynumsof 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] - 1or
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] - 1o或者
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]; } }
相关文章推荐
- [LeetCode]740. Delete and Earn
- [leetcode] 740. Delete and Earn
- 740. Delete and Earn
- 740. Delete and Earn
- 740. Delete and Earn
- 再从萌新开始-Leetcode每日题解-740. Delete and Earn
- Leetcode 740.Delete and Earn(第十六周作业)
- LeetCode | 740. Delete and Earn
- LeetCode740. Delete and Earn
- Leetcode 740. Delete and Earn
- 740. Delete and Earn
- 740. Delete and Earn
- Delete and Earn
- Delete and Earn
- 740. Delete and Earn
- 740. Delete and Earn
- 算法练习(32):Delete and Earn
- Delete and Earn
- 740. Delete and Earn
- [LeetCode] Delete and Earn 删除与赚取