Delete and Earn

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].

这是一题对我很有启发的动态规划。

解题思路:

先将数组的各个value放到另一个数组set中,其下标是value(和桶排序第一步一样),set中元素的值是index对应的数之和。(例如,数组1,3,3,4对应的桶,set[1] = 1,set[3] = 6,set[4] =4.)

设我们选中i元素时得到的分数是take[i],不选该元素时得到的分数是skip[i],动态规划的思路是:

第i个元素对应的take[i]是:

第i-1个元素不被选中的分数(也就是skip[i-1])+第i个元素的值(此时第i个元素被选中) 和

第i-1个元素被选中的分数(也就是take[i-1])(此时第i个元素不被选中)

之中最大的那个值。

第i个元素对应的skip[i]是:

第i-1个元素被选中的分数(也就是take[i-1])

一直到最后得出的take就是我们要的值。

class Solution {
public:
int deleteAndEarn(vector<int>& nums) {
int take = 0, skip = 0;
std::vector<int> set = (1001,0);
if(nums.empty()) return 0;
for(int i = 0; i < nums.size(); i++){
set[nums[i]] += nums[i];//初始化
}
for(int i = 0; i < nums.size(); i++){
/*
skip+set[i]:前一个元素不选的分数+当前元素选中的分数
take:前一个元素选中的分数(+当前元素不选的分数,也即是0)
*/
int temp = max(skip + set[i], take);
skip = take;//当前元素不选的分数 = 前一个元素选中的分数 + 0
take = temp;//更新当前元素选中的分数
}
return take;
}
};

[LeetCode]Delete and Earn题解(动态规划)的更多相关文章

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

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

  2. [LeetCode]Longest Palindromic Substring题解(动态规划)

    Longest Palindromic Substring: Given a string s, find the longest palindromic substring in s. You ma ...

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

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

  4. Leetcode 10. 正则表达式匹配 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  5. leetcode:House Robber(动态规划dp1)

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  6. C#版 - Leetcode 306. 累加数 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  7. C#版(击败100.00%的提交) - Leetcode 372. 超级次方 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. Leetcod ...

  8. C#版(打败97.89%的提交) - Leetcode 202. 快乐数 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  9. LC 740. Delete and Earn

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

随机推荐

  1. Spring-解决请求中文乱码问题

    解决spring请求中文乱码问题 1.web.xml添加编码拦截器 <filter> <filter-name>CharacterEncoding</filter-nam ...

  2. 关于CentOS-6的默认带的mysql启动和安装问题

    http://blog.csdn.net/arrowzz/article/details/24439731 以下纯复制粘贴: 一开始想自己一步一步从编译开始搭建一个lanmp环境: 从鸟哥的linux ...

  3. scrapy入门例子

    使用爬取http://quotes.toscrape.com/内容,网站内容很简单 一. 使用scrapy创建项目 scrapy startproject myscrapy1 scrapy gensp ...

  4. Jmeter Plugins----- Transactions per Second 配置项

    Jmeter Plugins---version 0.5.5 官方解释: Transactions per Second since 0.3.0 This graph shows the number ...

  5. springMVC传递一组对象的接受方式

    受益此大神:https://blog.csdn.net/cgd_8523/article/details/80022331 同时借鉴代码!!!! 我只用了一种方法,就记下这一种 需求:前台存在动态添加 ...

  6. Django分页的实现

    Django分页的实现 Django ORM  分页介绍 分页是网页浏览中常见到的一种形式,在数据量较大时,一个页面显示不全,采取分割数据由用户选择进行显示的方式. 基本实现 技术点 通过切片得到数据 ...

  7. [BZOJ5248][2018九省联考]一双木棋

    题目描述 https://www.lydsy.com/JudgeOnline/problem.php?id=5248   Solution 我们首先考虑放棋子的操作 发现它一定放棋子的部分是一个联通块 ...

  8. win10 ping不通所有地址

    电脑使用的很正常,是公司内网,但是在昨天测试数据库连接的时候,所有的地址都ping不通了.原先是可以ping通的,然后各种查,各种尝试. 清空dns缓存, cmd命令查看dns缓存:ipconfig ...

  9. ES6 三层for循环的链式写法

    假设有一个很复杂的数据,并且数据嵌套层数很多.如何避免用三层for循环呢? 有以下梨子,我们需要找到val值为12的,这个对象? 'use strict' let groups = [{ conten ...

  10. [转] gitlab 的 CI/CD 配置管理

    [From] http://blog.51cto.com/flyfish225/2156602 gitlab 的 CI/CD 配置管理 (二) 标签(空格分隔):运维系列 一:gitlab CI/CD ...