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们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- Spring框架的属性注入
1. 对于类成员变量,常用的注入方式有两种 * 构造函数注入(没有空的构造方法注入) * 属性setter方法注入(有空的构造方法注入) 2. 在Spring框架中提供了前两种的属性注入的方式 1. ...
- jq给动态生成的标签绑定事件的几种方法
经常遇到给动态生成的标签绑定事件不好用,自己简单测试总结了下,结论如下了: body> <!-- 下面是用纯动态方式生成标签 --> <div id="d2" ...
- 全屏API
by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=2679 二.相关文章以 ...
- Django模型之Meta详解
Django模型类的Meta是一个内部类,它用于定义一些Django模型类的行为特性.而可用的选项大致包含以下几类 abstract 这个属性是定义当前的模型是不是一个抽象类.所谓抽象类是不会对应数据 ...
- Error writing temporary file. Make sure your temp folder is valid
NSIS Error:Error writing temporary file. Make sure your temp folder is valid的解决 老婆用了自己的WIN7系统一段时 ...
- [SoapUI] 将科学计数法转化为普通数字,并且只保留小数点后几位
方案一: import java.text.NumberFormat class CompareHashMap { def regEx_Numeric = '-?[1-9]\\d*$|-?([1-9] ...
- jquery panel加载(dialog加载类似)
项目情况: 主页面用引用了公共头文件(包含easui.min.js),使用easyui的dialog(href方式)打开窗口(被打开的窗口页面是纯html片段,无body元素等,也引入了公共头文件), ...
- 【Linux】Jenkins配置和使用(二)
摘要 本章介绍Jenkins的简单使用,关于Jenkins的安装,参照[Linux]Jenkins安装(一) 事例说明:在linux环境下,安装的jenkins,集成svn,tomcat的环境,项目是 ...
- ajax序列化表单,再也不用通过data去一个个的传值了
jQuery的serialize()方法通过序列化表单值,创建URL编码文本字符串,我们就可以选择一个或多个表单元素,也可以直接选择form将其序列化 这样,我们就可以把序列化的值传给ajax()作为 ...
- IDEA SpringBoot Deprecated configuration property ‘server.servlet-path’
错误样式如图所示.说我这个版本中的这个标签是过时的. 解决: 出现这个问题后,这个标签被IDEA化成了黄线,同时,想使用server.servlet-path=*.html,配置servlet路径跳转 ...