740. Delete and Earn
Given an array
numsof integers, you can perform operations on the array.In each operation, you pick any
nums[i]and delete it to earnnums[i]points. After, you must delete every element equal tonums[i] - 1ornums[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.
Note:
- The length of
numsis at most20000.- Each element
nums[i]is an integer in the range[1, 10000].
Approach #1: DP. [C++]
class Solution {
public:
int deleteAndEarn(vector<int>& nums) {
int len = nums.size();
if (len == 0) return 0;
int r = *max_element(nums.begin(), nums.end());
vector<int> points(r+1, 0);
for (int num : nums)
points[num] += num;
return solve(points);
}
private:
int solve(const vector<int>& points) {
int dp1 = 0, dp2 = 0;
for (int point : points) {
int dp = max(dp2 + point, dp1);
dp2 = dp1;
dp1 = dp;
}
return dp1;
}
};
Analysis:
If we take nums[i], we can safely take all of its copies. We can't take any of copies of nums[i-1] and nums[i+1], This problem is reduced to 198 House Robber.
Houses[i] has all the copies of num whose value is i.
[3, 4, 2] -> [0, 2, 3, 4], rob([0, 2, 3, 4]) = 6
[2, 2, 3, 3, 3, 4] -> [0, 2*2, 3*3, 4], rob([0, 2*2, 3*3, 4]) = 9
Time complexity: O(n+r) reduction + O(r) solving rob = O(n + r)
Space complexity: O(r).
Reverence:
http://zxi.mytechroad.com/blog/dynamic-programming/leetcode-740-delete-and-earn/
740. Delete and Earn的更多相关文章
- LC 740. Delete and Earn
Given an array nums of integers, you can perform operations on the array. In each operation, you pic ...
- leetcode笔记(六)740. Delete and Earn
题目描述 Given an array nums of integers, you can perform operations on the array. In each operation, yo ...
- LeetCode 740. Delete and Earn
原题链接在这里:https://leetcode.com/problems/delete-and-earn/ 题目: Given an array nums of integers, you can ...
- 【leetcode】740. Delete and Earn
题目如下: Given an array nums of integers, you can perform operations on the array. In each operation, y ...
- [LeetCode]Delete and Earn题解(动态规划)
Delete and Earn Given an array nums of integers, you can perform operations on the array. In each op ...
- [LeetCode] Delete and Earn 删除与赚取
Given an array nums of integers, you can perform operations on the array. In each operation, you pic ...
- [Swift]LeetCode740. 删除与获得点数 | Delete and Earn
Given an array nums of integers, you can perform operations on the array. In each operation, you pic ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- ADF学习实用网站
ADF中所有组件工功能例子 http://jdevadf.oracle.com/adf-richclient-demo/faces/components/dialog.jspx;jsessionid= ...
- 在iframe框架中全屏不好使的原因
遇到的问题:我是在iframe框架中添加了一个插件在360和火狐中不好使,将allowfullscreen="true" 属性配置好就没问题了: 可能出现的原因:将allowful ...
- MongoDb进阶实践之四 MongoDB查询命令详述
一.引言 上一篇文章我们已经介绍了MongoDB数据库的最基本操作,包括数据库的创建.使用和删除数据库,文档的操作也涉及到了文档的创建.删除.更新和查询,当然也包括集合的创建.重命名和删除.有了这些基 ...
- 合成冷色黑暗恐怖魔法师图片的PS教程
教程主要使用Photoshop合成黑暗风格的魔法师施法场景,整体的场景效果以冷色风格为主,加上素材的叠加完成最终效果图,希望朋友可以喜欢.效果图: 先把背景拖进去,用工具吧字母弄掉. 加一个调色图层 ...
- HTML5新特性:范围样式
原文出处:http://blog.csdn.net/hfahe/article/details/7381141 Chromium 最近实现了一个HTML5的新特性:范围样式,又叫做< ...
- 学习socket的小例子
/************************************************************** 技术博客 http://www.cnblogs.com/itdef/ ...
- 如何配置JDK?
有很多人,java都下载好,却因不会配置JDK,而无法编程.今天巩固就来教大家配置JDK. 第一步:将下载好的java放在D盘(最好不要占用C盘). 第二步:右击我的电脑,选择"属性 ...
- JavaScript 代码小片段
1.获取对象 obj 的所有属性(自有属性和继承属性),保存到数组 lst 中 //获取对象obj的所有属性(自有属性和继承属性),保存到数组lst 中 var lst = []; function ...
- 2018.10.13 bzoj4008: [HNOI2015]亚瑟王(概率dp)
传送门 马上2点考初赛了,心里有点小紧张. 做道概率dp压压惊吧. 话说这题最开始想错了. 最开始的方法是考虑f[i][j]f[i][j]f[i][j]表示第iii轮出牌为jjj的概率. 然后用第ii ...
- foreach循环赋值问题
foreach ($list as $key=>$val){ $data=array();//这一个一定要加上不然循环后,modify_one,modify_two都会赋值 if ($val[' ...