题目如下:

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

解题思路:动态规划。首先我们把可以获得积分的删除为主动删除,不能获得积分的删除为被动删除。记dp[i][0] = v 表示被动删除值为i时,在nums中所有元素值为1~i时可以获得最大积分v,而dp[i][1] = v为主动删除i时获得的最大积分,那么有,

1. 被动删除i:那么i-1只能是主动删除,有dp[i][0] = dp[i-1][1]

2.主动删除i:i-1只能是被动删除,有dp[i][1] = dp[i-1][0] + 删除i可获得的积分

代码如下:

class Solution(object):
def deleteAndEarn(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) == 0: return 0
dic = {}
max_val = 0
for i in nums:
max_val = max(max_val,i)
dic[i] = dic.setdefault(i,0) + 1
uniq = range(1,max_val+1) dp = [[0] * 2 for _ in uniq]
#0 : won't pick; 1:pick
dp[0][0] = 0
dp[0][1] = uniq[0] * dic.get(uniq[0],0)
for i in range(1,len(dp)):
dp[i][0] = max(dp[i-1][0],dp[i-1][1])
dp[i][1] = dp[i-1][0] + uniq[i] * dic.get(uniq[i],0)
return max(dp[-1])

【leetcode】740. Delete and Earn的更多相关文章

  1. 【leetcode】955. Delete Columns to Make Sorted II

    题目如下: We are given an array A of N lowercase letter strings, all of the same length. Now, we may cho ...

  2. 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...

  3. 【LeetCode】944. Delete Columns to Make Sorted 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. 【LeetCode】450. Delete Node in a BST 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 日期 题目地址:https://leetcode ...

  5. 【LeetCode】583. Delete Operation for Two Strings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 【LeetCode】237. Delete Node in a Linked List

    题目: Write a function to delete a node (except the tail) in a singly linked list, given only access t ...

  7. 【Leetcode】583. Delete Operation for Two Strings

    583. Delete Operation for Two Strings Given two words word1 and word2, find the minimum number of st ...

  8. 【leetcode】1273. Delete Tree Nodes

    题目如下: A tree rooted at node 0 is given as follows: The number of nodes is nodes; The value of the i- ...

  9. 【leetcode】960. Delete Columns to Make Sorted III

    题目如下: We are given an array A of N lowercase letter strings, all of the same length. Now, we may cho ...

随机推荐

  1. 【C/C++语言】指针常量与常量指针的区别

    三个名词虽然非常绕嘴,不过说的非常准确.用中国话的语义分析就可以很方便地把三个概念区分开. 一) 常量指针. 常量是形容词,指针是名词,以指针为中心的一个偏正结构短语.这样看,常量指针本质是指针,常量 ...

  2. hadoop的目录结构介绍

    hadoop的目录结构介绍 解压缩hadoop 利用tar –zxvf把hadoop的jar包放到指定的目录下. tar -zxvf /home/software/aa.tar.gz -C /home ...

  3. [python] 执行 dos 命令

    python的os模块 os模块调用CMD命令有两种方式:os.popen(),os.system(). 都是用当前进程来调用. os.system是无法获取返回值的.当运行结束后接着往下面执行程序. ...

  4. [转帖]How does a CPU work?

    How does a CPU work? https://milapneupane.com.np/2019/07/06/how-does-a-cpu-work/ CPU, also known as ...

  5. Xtrabackup innobackupex

    Xtrabackup是由percona提供的mysql数据库备份工具,据官方介绍,这也是世界上惟一一款开源的能够对innodb和xtradb数据库进行热备的工. Xtrabackup中主要包含两个工具 ...

  6. Type类的使用

    Type类的使用(类反射)通过类获得Type: Type t = typeof(Person)通过实例对象获得类的Type: Type t = p.GetType()获取Type的方法:MethodI ...

  7. Codeforces 1203F2. Complete the Projects (hard version)

    传送门 首先对于 $b>0$ 的工作显然有个贪心,把 $b>0$ 的按 $a$ 从小到大排序 把能做的都做了,然后得到一个最大等级 剩下就是考虑 $b<0$ 的工作了,看到数据显然可 ...

  8. Iterator<E>接口

    https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html public interface Iterator<E> ...

  9. 无法加载文件 C:\Program Files\nodejs\npm.ps1,因为在此系统上禁止运行脚本。

    今天使用npm安装插件时出现了以下错误: 经查,原因:现用执行策略是 Restricted(默认设置) 解决办法: 1.win+X键,使用管理员身份运行power shell 2.输入命令:set-e ...

  10. 部署Flannel网络

    部署Flannel网络 部署flannel网络需要执行以下步骤: 1)写入分配的子网段到etcd,供flanneld使用 2)下载二进制包 3)配置Flannel 4)systemd管理Flannel ...