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们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- linux-Centos 7下mysql 5.7.9的rpm包安装
操作系统:Centos 7.1 mysql数据库版本:mysql5.7.18 1.安装新版mysql之前,我们需要将系统自带的mariadb-lib卸载 [root@123 ~]# rpm -qa|g ...
- [freeCodeCamp] solution to HTTP JSON API SERVER passed!
var http = require('http') var url = require('url') function parsetime (time) { return { hour: time. ...
- Legendre多项式
Legendre多项式 时间限制: 1 Sec 内存限制: 128 MB 题目描述 Legendre多项式的递归公式
- Zookeeper 系列(五)Curator API
Zookeeper 系列(五)Curator API 一.Curator 使用 Curator 框架中使用链式编程风格,易读性更强,使用工程方法创建连接对象使用. (1) CuratorFramewo ...
- 设计神器 - 摹客设计系统上线了 | 晒出你的设计规范,赢iPad Pro!
在国内,设计规范也许还是个不太常用的概念,但是如果你正好有参与互联网公司的产品设计,你应该早就已经体会到设计规范的重要性了.UI设计师总是要花费大量的时间和精力向开发描述一大堆设计细节,但是产品最后呈 ...
- python数据类型4
一浮点数 什么叫做浮点数:浮点数就相当于小数,但是浮点数不包括无限循环又不重复的小数. 小数分为 有限小数和无限小数 无限小数又分为 无限循环小数和无限不循环小数 而 浮点数就是有限小数和无限循环小数 ...
- 如何通过cmd命令进入到某个硬盘的文件夹
1.使用快捷键win+R打开运行窗口,并输入cmd回车 2.进入到某个磁盘:在命令提示符中输入d:(代表的的是进入D盘的根目录)并回车 3.接着在cmd中输入dir(dir是directory目录的简 ...
- 【Unity】1.1 安装Unity 5.3.4 开发环境
分类:Unity.C#.VS2015 创建日期:2016-03-23 一.简介 Unity分个人版(Personal)和专业版(Pro).个人版是免费的(部分高级功能受限,但初学者也用不到它),Pro ...
- BZOJ 1008 [HNOI2008]越狱 (简单排列组合 + 快速幂)
1008: [HNOI2008]越狱 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 10503 Solved: 4558[Submit][Status ...
- 常见XML解析器
xpp3 官网 http://www.extreme.indiana.edu/xgws/xsoap/xpp/ 简介 Xml Pull Parser (in short XPP) is a stream ...