【leetcode】1218. Longest Arithmetic Subsequence of Given Difference
题目如下:
Given an integer array
arrand an integerdifference, return the length of the longest subsequence inarrwhich is an arithmetic sequence such that the difference between adjacent elements in the subsequence equalsdifference.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的更多相关文章
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
- 【leetcode】1027. Longest Arithmetic Sequence
题目如下: Given an array A of integers, return the length of the longest arithmetic subsequence in A. Re ...
- 【leetcode】300.Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- 【LeetCode】594. Longest Harmonious Subsequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计次数 日期 题目地址:https://leetc ...
- 【LeetCode】521. Longest Uncommon Subsequence I 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】516. Longest Palindromic Subsequence 最长回文子序列
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 代码 刷题心得 日期 题目地址:https://le ...
- 【LeetCode】300. Longest Increasing Subsequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】521. Longest Uncommon Subsequence I
problem 521. Longest Uncommon Subsequence I 最长非共同子序列之一 题意: 两个字符串的情况很少,如果两个字符串相等,那么一定没有非共同子序列,反之,如果两个 ...
- 【leetcode】1143. Longest Common Subsequence
题目如下: Given two strings text1 and text2, return the length of their longest common subsequence. A su ...
随机推荐
- 好问题:count(1)、count(*)、count(列)有什么区别?
执行效果: 1. count(1) and count(*) 当表的数据量大些时,对表作分析之后,使用count(1)还要比使用count(*)用时多了! 从执行计划来看,count(1)和coun ...
- springboot2.0处理自定义异常始终返回json
1. 编写自定义异常类 package cn.jfjb.crud.exception; /** * @author john * @date 2019/11/24 - 9:48 */ public c ...
- x系统清理/tmp/文件夹的原理
转自:http://www.opsers.org/base/clean-up-on-the-linux-system-tmp-folder-you-may-want-to-know.html§ 我们知 ...
- 一遍记住 8 种排序算法与 Java 代码实现
☞ 程序员进阶必备资源免费送「21种技术方向!」 ☜ 作者:KaelQ, www.jianshu.com/p/5e171281a387 1.直接插入排序 经常碰到这样一类排序问题:把新的数据插入到已经 ...
- IIS发布出现[Microsoft][ODBC 驱动程序管理器] 在指定的 DSN 中,驱动程序和应用程序之间的体系结构不匹配
一,原因是系统DSN的配置平台位数跟系统的位数不一致(PS:确认你有没有安装对应系统的驱动.本文是34位和64位驱动都安装了) 二,解决方法,我们必须在 <控制面板---管理工具>找到对应 ...
- 18.AutoMapper 之条件映射(Conditional Mapping)
https://www.jianshu.com/p/8ed758ed3c63 条件映射(Conditional Mapping) AutoMapper 允许你给属性添加条件,只有在条件成立的情况下该成 ...
- 一文简单理解package-lock.json
根据官方文档,https://docs.npmjs.com/files/package-lock.json 这个package-lock.json 是在 `npm install`时候生成一份文件,用 ...
- cherrypy
十多年来,Web 程序设计人员一直使用 CGI 将应用程序连接到 Web 服务器和另一端的 Web 浏览器.有很多理由建议使用 CGI:它可以与任何编程语言一起使用,并且它在 Web 服务器和宿主服务 ...
- shell脚本之删除内容相同的重复文件
#!/bin/bash #!当前文件夹下,删除内容相同的重复文件,只保留重复文件中的一个. ls -lS --time-style=long-iso | awk 'BEGIN{ getline;get ...
- System.Windows.Forms.Application.DoEvents();
关于Application.DoEvents()的小研究 在MSDN中的备注是: 当运行 Windows 窗体时,它将创建新窗体,然后该窗体等待处理事件.该窗体在每次处理事件时,均将处理与该事件关联的 ...