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们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- vue-cli启动本地服务,局域网下通过ip访问不到的原因
1.问题描述: 新开发了一个vue-cli项目,想通过手机查看效果,发现访问不到,ip地址和端口号都没错但是手机访问不到,在本机电脑浏览器输入ip端口号一样访问不到,只能通过localhost:808 ...
- [PHP] 转义字符 Escape character
\n is a symbol for new line \t is a symbol for tab and \r is for 'return'
- Laravel 5.x 启动过程分析
Posted on 2015年9月11日 by 学院君 1.初始化Application 1.1 注册基本绑定 app -> Application实例(Illuminate\Foundat ...
- 创建一个子进程---vfork
子.父进程共享数据段与堆栈段 函数原型:pid_t vfork(void) 返回值:子进程中返回0,父进程中返回子进程ID,出错返回-1. 注意: vfork创建的进程是按先子进程后父进程的顺序执行的 ...
- 简明PR教程
注意:本文供培训使用且仅为第一版 作者也不打算继续更新 本篇文章最早是在为内部培训时所编写的文章 有些疏漏且没有进行校正等工作 我尽力用最简单通俗的语言给大家介绍PR的使用方法 简明PR教程 1.编辑 ...
- Jfinal框架是什么框架?适用于什么项目呢?
Jfinal框架是什么框架?适用于什么项目呢? jfinal 基于spring MVC研发的框架,操作简单.节省代码,适用于所有web项目.适合中小型项目开发.10分钟写出一个页面的增删改查.目前所在 ...
- C++动态分配内存(new)和撤销内存(delete)
在软件开发过程中,常常需要动态地分配和撤销内存空间,例如对动态链表中结点的插入与删除.在C语言中是利用库函数malloc和free来分配和撤销内存空间的.C++提供了较简便而功能较强的运算符new和d ...
- 理解指令的restrict属性(转)
restrcit属性说明 restrict: EACM中的任意一个之母.它是用来限制指令的声明格式的. E - 元素名称:<my-directive></my-directive&g ...
- shell中$(( ))、$( )与${ }的区别
转载自:http://blog.sina.com.cn/s/blog_4da051a60102uwda.html 命令替换 在bash中,$( )与` `(反引号)都是用来作命令替换的. 命令替换与变 ...
- I2C笔记
SCL:上升沿将数据输入到每个EEPROM器件中:下降沿驱动EEPROM器件输出数据.(边沿触发) SDA:双向数据线,为OD门,与其它任意数量的OD与OC门成"线与"关系. ...