题目描述:

class Solution:
def longestSubsequence(self, arr: List[int], difference: int) -> int:
dp = dict()
for a in arr:
pre = a - difference
if pre in dp:
dp[a] = max(dp.get(a, 0), dp[pre] + 1)
else:
dp[a] = 1
return max([x for _, x in dp.items()])

优化:

class Solution(object):
def longestSubsequence(self, A, D):
count = collections.defaultdict(int)
for x in A:
#prev + d = x
# prev = x-d
count[x] = max(count[x], count[x-D] + 1)
return max(count.values())

leetcode-157周赛-5214-最长定差子序列的更多相关文章

  1. LeetCode 5214. 最长定差子序列(Java)HashMap

    题目: 5214. 最长定差子序列 给你一个整数数组 arr 和一个整数 difference,请你找出 arr 中所有相邻元素之间的差等于给定 difference 的等差子序列,并返回其中最长的等 ...

  2. leetcode 1218. 最长定差子序列

    问题描述 给你一个整数数组 arr 和一个整数 difference,请你找出 arr 中所有相邻元素之间的差等于给定 difference 的等差子序列,并返回其中最长的等差子序列的长度.   示例 ...

  3. [LeetCode] Longest Palindromic Subsequence 最长回文子序列

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  4. [LeetCode] Longest Harmonious Subsequence 最长和谐子序列

    We define a harmonious array is an array where the difference between its maximum value and its mini ...

  5. LeetCode 594. Longest Harmonious Subsequence (最长的协调子序列)

    We define a harmonious array is an array where the difference between its maximum value and its mini ...

  6. [LeetCode] Longest Uncommon Subsequence I 最长非共同子序列之一

    Given a group of two strings, you need to find the longest uncommon subsequence of this group of two ...

  7. [LeetCode] 409. Longest Palindrome 最长回文

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  8. [LeetCode] 516. Longest Palindromic Subsequence 最长回文子序列

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  9. LeetCode 516——最长回文子序列

    1. 题目 2. 解答 与最长回文子串类似,我们可以用动态规划来求解这个问题,只不过这里的子序列可以不连续.我们定义状态 state[i][j] 表示子串 s[i, j] 的最长回文子序列长度,那么状 ...

随机推荐

  1. layui的选项卡(tab)的问题

    当页面打开单个tab时,操作栏显示: 当页面打开多个tab时,会发现操作栏与下面第一个tab显示的操作栏类型一样,并且操作栏的按钮无作用 第一个标签操作栏显示: 产生这样的原因:使用layui时,每个 ...

  2. 关于Ms Sql server 表列等是否存在

    select object_id('名称') ,object_id('名称','类型') 1. 等价于 select * from sys.objects where name ='名称' selec ...

  3. LCA的 Trajan 算法

    参考博客 参考博客 根据博客的模拟,就可以知道做法和思想. 现在就是实现他. 例题 :hdu  2586  题意:m 个询问,x  到  y  的距离,我们的思想就是求出:x到根的距离+y到根的距离- ...

  4. pytest_fixture--scope="session"

    import pytest@pytest.fixture(scope="session")def login(): print("\n输入用户名密码登陆! configt ...

  5. 本地项目上传github

    (1)github上面新建仓库 (2) 1. git init //初始化仓库 2. git add .(文件name) //添加文件到本地仓库 3. git commit -m "firs ...

  6. react css拓展 使用less

    react 之中使用less 其实质只需要看一下resct 使用css的配置项,就能明白个大概了  第一步   还是下载 npm i  less less-loader -save 下载less 和 ...

  7. 微信小程序学习之navigate(1)navigateTo方法与navigateBack方法对于page生命周期不同的触发影响

    小程序的每个页面都有一些生命周期,每个生命周期由分别有着不同的生命周期钩子函数.而我们的业务逻辑写在这些生命周期的钩子函数中,那么弄清楚那种情形下会触发那些生命周期钩子函数就非常重要了 先上一段代码 ...

  8. leetcood学习笔记-107-二叉树的层次遍历二

    题目描述: 方法一: class Solution(object): def levelOrderBottom(self, root): """ :type root: ...

  9. charles抓取数据

    charles抓包 抓取手机app的数据 charles设置 help--->SSL Proxying--->Install Charles Root Certificate 1.给电脑下 ...

  10. 帝国cms把文章加入到收藏夹代码

    内容模板加这个:<a href="[!--news.url--]e/member/fava/add/?classid=[!--classid--]&id=[!--id--]&q ...