题目描述:

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. Spring IOC源码分析(二):Bean工厂体系结构设计

    一. 概述 Spring容器通常指的是ApplicationContext的体系结构设计,即整个Spring框架的IOC功能,是通过ApplicationContext接口实现类来提供给应用程序使用的 ...

  2. B. Light bulbs(2019 ICPC上海站)

    There are NN light bulbs indexed from 00 to N-1N−1. Initially, all of them are off. A FLIP operation ...

  3. 常用css3属性的ie兼容查看

    记录一下关于css3的各种常用属性对ie各版本浏览器的兼容程度: 最低可兼容ie7 最低可兼容ie8 最低可兼容ie9 最低可兼容ie10 position:fixed clip E:first-le ...

  4. linux每日命令(3):which命令

    这个命令我也神佑体会它的用处,在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索: which     查看可执行文件的位置. whereis 查看文件的位置. locate ...

  5. 制作一个自己的xhprof测试平台

    1 1.首先安装php开发环境,比如lnmp. 2.安装xhprof ps: 记住从github上面下载(https://github.com/phacility/xhprof), 不要从pecl.p ...

  6. 2018-2-13-win10-uwp-让焦点在点击在页面空白处时回到textbox中

    title author date CreateTime categories win10 uwp 让焦点在点击在页面空白处时回到textbox中 lindexi 2018-2-13 17:23:3 ...

  7. eureka多实例,模拟多台机器

    本文只有一个eureka server项目,运行在不同的端口,模拟两台eureka服务.开发使用eclipse 4.8 先说pom.xml文件,如果出现问题,首先考虑springboot和其他包版本冲 ...

  8. (补充)9.Struts2中的OGNL表达式

    OGNL表达式概述 1. OGNL是Object Graphic Navigation Language(对象图导航语言)的缩写 * 所谓对象图,即以任意一个对象为根,通过OGNL可以访问与这个对象关 ...

  9. Number 的扩展

    Number.parseInt(), Number.parseFloat() ES6 将全局方法parseInt()和parseFloat(),移植到Number对象上面,行为完全保持不变. Numb ...

  10. Batch - windows batch 常用命令(cheat sheet)

    原文地址:https://www.oschina.net/code/snippet_158297_4964 1 echo 和 @ 回显命令 @ #关闭单行回显 echo off #从下一行开始关闭回显 ...