题目描述:

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. MySQL在win10以及linux下数据库的备份以及还原

    MySQL在win环境或者linux下的命令都是一样的,只是路径不一致而已 MySQL的备份 (非必须)命令行进入MySQL的bin目录 输入命令:mysqldump -u userName -p d ...

  2. 【POJ】1287 Networking

    题目链接:http://poj.org/problem?id=1287 题意:n个点,m条网线长度.求构成网络的最小网线长度. 题解:最小生成树裸题.套板子. 代码: #include<iost ...

  3. java反射机制以及应用

    JAVA反射机制+动态运行编译期不存在的JAVA程序 一.有关JAVA反射 在运行期间,在不知道某个类A的内部方法和属性时,能够动态的获取信息.获取类或对象的方法.属性的功能,称之为反射. 1.相关类 ...

  4. Google Fuchsia

    Fuchsia是Google开发的操作系统[1].和以前Google开发的操作系统,如基于Linux内核的Chrome OS和Android等不同,Fuchsia基于新的名为Zircon的微内核[2] ...

  5. [转载]A星寻路算法介绍

    转载自:http://www.raywenderlich.com/zh-hans/21503/a%E6%98%9F%E5%AF%BB%E8%B7%AF%E7%AE%97%E6%B3%95%E4%BB% ...

  6. 发现一个新的远程软件 gotohttp

    之前直到远程桌面连接是TeamViewer 替换的原因是: 被控制端版本 11.0.x (很久以前安装的),而我本地的Teamviewer是 14.x, 去连接,好像提示被控制端的版本太低:本地使用 ...

  7. 如何定义一个BUG

    一.划分一个bug的等级 bug等级主要分为致命.严重.一般.轻微或者建议四个等级: 1.致命错误:系统无法执行.崩溃或严重资源不足.应用模块无法启动或异常退出.无法测试.造成系统不稳定.价值较高功能 ...

  8. 关于sublime使用中写less代码高亮显示问题

    一开始在没有配置的情况下在sublime中写less代码是不会有高亮显示的.下面说一下配置过程 一.安装Less2Css模块 打开sublime,ctrl+shift+p,输入package cont ...

  9. Web API 接口参考

    Web API 接口参考:https://developer.mozilla.org/zh-CN/docs/Web/API

  10. NX二次开发-UFUN将实体放入STL文件中函数UF_STD_put_solid_in_stl_file

    NX9+VS2012 #include <uf.h> #include <uf_obj.h> #include <uf_modl.h> #include <u ...