题目如下:

Given an integer array arr and an integer difference, return the length of the longest subsequence in arr which is an arithmetic sequence such that the difference between adjacent elements in the subsequence equals difference.

Example 1:

Input: arr = [1,2,3,4], difference = 1
Output: 4
Explanation: The longest arithmetic subsequence is [1,2,3,4].

Example 2:

Input: arr = [1,3,5,7], difference = 1
Output: 1
Explanation: The longest arithmetic subsequence is any single element.

Example 3:

Input: arr = [1,5,7,8,5,3,4,2,1], difference = -2
Output: 4
Explanation: The longest arithmetic subsequence is [7,5,3,1].

Constraints:

  • 1 <= arr.length <= 10^5
  • -10^4 <= arr[i], difference <= 10^4

解题思路:记dic[i] = v 表示元素i是当前组成公差difference的第v个元素。只要遍历arr,判断每个元素i - different 是否存在于dic中;如果存在,dic[i] = dic[i-difference] + 1 ,否则dic[i] = 1。最后求出dic中value的最大值即可。

代码如下:

class Solution(object):
def longestSubsequence(self, arr, difference):
"""
:type arr: List[int]
:type difference: int
:rtype: int
"""
dic = {}
res = 1
for i in arr:
if (i - difference) in dic:
dic[i] = dic[i-difference] + 1
else:
dic[i] = 1
res = max(res, dic[i])
return res

【leetcode】1218. Longest Arithmetic Subsequence of Given Difference的更多相关文章

  1. 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

  2. 【leetcode】1027. Longest Arithmetic Sequence

    题目如下: Given an array A of integers, return the length of the longest arithmetic subsequence in A. Re ...

  3. 【leetcode】300.Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  4. 【LeetCode】594. Longest Harmonious Subsequence 解题报告(Python & C++)

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

  5. 【LeetCode】521. Longest Uncommon Subsequence I 解题报告(Python)

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

  6. 【LeetCode】516. Longest Palindromic Subsequence 最长回文子序列

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 代码 刷题心得 日期 题目地址:https://le ...

  7. 【LeetCode】300. Longest Increasing Subsequence 解题报告(Python & C++)

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

  8. 【leetcode】521. Longest Uncommon Subsequence I

    problem 521. Longest Uncommon Subsequence I 最长非共同子序列之一 题意: 两个字符串的情况很少,如果两个字符串相等,那么一定没有非共同子序列,反之,如果两个 ...

  9. 【leetcode】1143. Longest Common Subsequence

    题目如下: Given two strings text1 and text2, return the length of their longest common subsequence. A su ...

随机推荐

  1. 关机命令 shutdown

    参考资料:[http://jingyan.baidu.com/article/49ad8bce705f3f5834d8faec.html]

  2. python学习之函数(一)

    4.4函数 面向过程编程的缺点: ​ 1.代码重复: ​ 2.代码可描述性不高: 4.4.1 函数初识 ​ 函数是对代码块动作和功能的封装定义:函数是以功能为导向,一个为函数封装一个功能. ​ 优点: ...

  3. MySQL 数据库下载

    地址链接: msi:https://dev.mysql.com/downloads/installer/ zip:https://downloads.mysql.com/archives/commun ...

  4. (4.13)mysql备份原理(转)

    关键词:mysqldump原理,--single-transaction,mysql备份原理 转自:https://www.cnblogs.com/cchust/p/5452557.html MySQ ...

  5. spring+redis实例(二)

    这一篇redis实例是基于序列化储存-(写入对象,读取对象) 在spring+redis(一)中我们介绍了在spring中怎么去操作储存redis,基于string的储存,今天我们介绍一下redis基 ...

  6. P1397 [NOI2013]矩阵游戏

    传送门 首先显然可以矩乘快速幂然后 $T$ 飞 看一眼题解发现因为这一题矩阵的特殊性所以可以对矩阵的次数欧拉降幂 然而我并不懂证明,所以我选择暴力乱搞的做法 十进制快速幂,然后注意一下常数,还有矩阵乘 ...

  7. --解决Lock wait timeout exceeded; try restarting transaction

    --解决Lock wait timeout exceeded; try restarting transaction select * from information_schema.innodb_t ...

  8. css练习小总结

    1.div水平居中:/*设置div宽度后,margin:0 auto:*/2.div显示在一行:/*float:left:*/ 3.text-align:/*后面标签里的内容水平居中:*/4.alig ...

  9. vue打包时,assets目录 和static目录下文件的处理区别(nodeModule中插件源码修改后,打包后的文件应放在static目录)

    为了回答这个问题,我们首先需要了解Webpack如何处理静态资产.在 *.vue 组件中,所有模板和CSS都会被 vue-html-loader 及 css-loader 解析,并查找资源URL.例如 ...

  10. Java日志打印方法

    一.使用log4j打印日志 1. 下载log4j.jar和commons-logging.jar.     log4j.jar下载地址:http://logging.apache.org/log4j/ ...