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.

Note:

  • The length of nums is at most 20000.
  • 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的更多相关文章

  1. LC 740. Delete and Earn

    Given an array nums of integers, you can perform operations on the array. In each operation, you pic ...

  2. leetcode笔记(六)740. Delete and Earn

    题目描述 Given an array nums of integers, you can perform operations on the array. In each operation, yo ...

  3. LeetCode 740. Delete and Earn

    原题链接在这里:https://leetcode.com/problems/delete-and-earn/ 题目: Given an array nums of integers, you can ...

  4. 【leetcode】740. Delete and Earn

    题目如下: Given an array nums of integers, you can perform operations on the array. In each operation, y ...

  5. [LeetCode]Delete and Earn题解(动态规划)

    Delete and Earn Given an array nums of integers, you can perform operations on the array. In each op ...

  6. [LeetCode] Delete and Earn 删除与赚取

    Given an array nums of integers, you can perform operations on the array. In each operation, you pic ...

  7. [Swift]LeetCode740. 删除与获得点数 | Delete and Earn

    Given an array nums of integers, you can perform operations on the array. In each operation, you pic ...

  8. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  9. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

随机推荐

  1. struts2框架之自定义拦截器和配置

    struts框架中也存在拦截器,只不过系统自动调用.框架自带的拦截器的配置文件所在的位置为: java Resources--->Libraries--->struts2-core-2.3 ...

  2. spring框架中工厂方法的创建和销毁

    1.编写接口UserSerivce: public interface UserService { public void sayHello(); } 2.编写实实现接口的方法,在该方法中除了要实现接 ...

  3. count(distinct) 与group by 浅析

    x在传统关系型数据库中,group by与count(distinct)都是很常见的操作.count(distinct colA)就是将colA中所有出现过的不同值取出来,相信只要接触过数据库的同学都 ...

  4. linux-git服务搭建

    第一步,安装git: 源码安装参考:http://www.cnblogs.com/syuf/p/9151115.html 第二步,创建一个git用户,用来运行git服务: $ sudo adduser ...

  5. 在原型设计上,UI和UX设计师有哪三个区别?

    原型设计在日常的软件开发过程中是必不可少的,不管是UI还是UX设计师,很多工作都会涉及到原型设计.那么这两类设计师在设计原型的时候表现出了哪些的不同点呢?今天就让我们来讨论一下,先说说我发现的3个不同 ...

  6. li.active2有加强重要性的效果。

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. Tomcat之Windows环境下配置多个服务器

    在应对多项目多端口的情况配置一个服务器是远不能满足开发条件的.例如微信公众号回调域名只接受80端口,而其他项目一般为默认的8080或者自定义的其他的端口. 废话多说,直入主题 准备条件:tomcat文 ...

  8. 在用easyui中做CRUD功能时,当删除一行或多行数据后再点击修改会提示你选中了多行,如何解决这个bug了?

    在用easyui中做CRUD功能时,当删除一行或多行数据后再点击修改会提示你选中了多行,如何解决这个bug了? 在删除成功后,加上这句话就可以了:$("#dg").datagrid ...

  9. 2018.09.27 bzoj4300: 绝世好题(二进制dp)

    传送门 简单dp. 根据题目的描述. 如果数列bn{b_n}bn​合法. 那么有:bi−1b_{i-1}bi−1​&bi!=0b_i!=0bi​!=0,因此我们用f[i]f[i]f[i]表示数 ...

  10. asp.net 中长尾链接实现推送 -- comet

    一般需求推送服务时,都会去第三方拿推送组件,如”极光“,”百度“,”小米"什么的,自己用.net实现推送服务端需要面对很多问题,比如C10K,但是企业内部使用往往用不了10K的链接,有个1K ...