【leetcode】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].
解题思路:动态规划。首先我们把可以获得积分的删除为主动删除,不能获得积分的删除为被动删除。记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的更多相关文章
- 【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 ...
- 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...
- 【LeetCode】944. Delete Columns to Make Sorted 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】450. Delete Node in a BST 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 日期 题目地址:https://leetcode ...
- 【LeetCode】583. Delete Operation for Two Strings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【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 ...
- 【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 ...
- 【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- ...
- 【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 ...
随机推荐
- Springboot 上传报错: Failed to parse multipart servlet request; nested exception is java.lang.IllegalStateException: The multi-part request contained parameter data (excluding uploaded files) that exceede
Failed to parse multipart servlet request; nested exception is java.lang.IllegalStateException: The ...
- sql for xml path 和group_concat
/*sqlserver*/ select Id,AId,(SELECT IMEI+',' FROM Device as s WHERE s.Id=c.Id For XML Path('')) AS I ...
- Java 架构师 -- 必读书单
“学习的最好途径就是看书“,这是我自己学习并且小有了一定的积累之后的第一体会. 个人认为看书有两点好处: 1.能出版出来的书一定是经过反复的思考.雕琢和审核的,因此从专业性的角度来说,一本好书的价值远 ...
- Java程序员的职业发展道路 附:大型网站 -- 架构技能图谱(Java版)
职业发展道路基本有3条: 第一条路线(技术专精): 初级Java开发---中级--高级---项目主管--Java项目经理---网站架构师----资深专家 第二条路线(技术转产品):初级Java开发-- ...
- 一篇文章搞懂python2、3编码
说在前边: 编码问题一直困扰着每一个程序员的编程之路,如果不将它彻底搞清楚,那么你的的这条路一定会走的格外艰辛,尤其是针对使用python的程序员来说,这一问题更加显著, 因为python有两个版本, ...
- sqlalchemy的join使用
——.先看mysql的join链接方法 #encoding: utf-8 from sqlalchemy import create_engine,Column,Integer,String,Floa ...
- RramSim2
1.官网 http://www.computer.org/csdl/letters/ca/2011/01/lca2011010016-abs.html 2.介绍 http://blog.csdn.ne ...
- [转帖]浪潮信息最大供应商英特尔(Intel):2018 年采购额 145.76 亿元
浪潮信息最大供应商英特尔(Intel):2018 年采购额 145.76 亿元 https://t.cj.sina.com.cn/articles/view/3172142827/bd130eeb01 ...
- [转帖]国科微发布纯正国产SSD主控 龙芯IP内核,速度可达500MB/s
国科微发布纯正国产SSD主控龙芯IP内核,速度可达500MB/s https://www.expreview.com/68071.html 自主内核 龙芯处理器. 2019.4. 在存储芯片领域,中国 ...
- Linux系统下部署Tomcat服务器
详细步骤如下: 1.下载xshell5和xftp5并安装,登录连接服务器,新建会话窗口: 2.安装配置JDK1.8,具体操作方法如下: (1)下载JDK地址:http://www.oracle.com ...