题目如下:

Given an array A of integers, return the length of the longest arithmetic subsequence in A.

Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).

Example 1:

Input: [3,6,9,12]
Output: 4
Explanation:
The whole array is an arithmetic sequence with steps of length = 3.

Example 2:

Input: [9,4,7,2,10]
Output: 3
Explanation:
The longest arithmetic subsequence is [4,7,10].

Example 3:

Input: [20,1,15,3,10,5,8]
Output: 4
Explanation:
The longest arithmetic subsequence is [20,15,10,5].

Note:

  1. 2 <= A.length <= 2000
  2. 0 <= A[i] <= 10000

解题思路:首先用字典记录A中每个元素出现的下标,接下来求出任意A[i]与A[j]的差值d,依次判断A[j] += d是否存在于A中,并且要求A[j] + d的下标的最小值要大于j,最终即可求出最长的等差数列。

代码如下:

class Solution(object):
def longestArithSeqLength(self, A):
"""
:type A: List[int]
:rtype: int
"""
import bisect
res = 0
dic = {}
for i,v in enumerate(A):
dic[v] = dic.setdefault(v,[]) + [i]
for i in range(len(A)):
for j in range(i+1,len(A)):
count = 2
diff = A[j] - A[i]
next = A[j] + diff
smallestInx = j
while True:
if next not in dic:
break
inx = bisect.bisect_right(dic[next],smallestInx)
if inx == len(dic[next]):
break
smallestInx = dic[next][inx]
next = next + diff
count += 1
res = max(res,count)
return res

【leetcode】1027. Longest Arithmetic Sequence的更多相关文章

  1. 【leetcode】1218. Longest Arithmetic Subsequence of Given Difference

    题目如下: Given an integer array arr and an integer difference, return the length of the longest subsequ ...

  2. 【LeetCode】128. Longest Consecutive Sequence

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

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

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

  4. LeetCode 1027. Longest Arithmetic Sequence

    原题链接在这里:https://leetcode.com/problems/longest-arithmetic-sequence/ 题目: Given an array A of integers, ...

  5. 【LeetCode】159. Longest Substring with At Most Two Distinct Characters

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Given a string S, find the length of the long ...

  6. 【leetcode】300.Longest Increasing Subsequence

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

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

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

  8. 【leetcode】1048. Longest String Chain

    题目如下: Given a list of words, each word consists of English lowercase letters. Let's say word1 is a p ...

  9. 【LeetCode】409. Longest Palindrome 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:字典统计次数 方法二:HashSet 方法三 ...

随机推荐

  1. flex embed 使用

    Flex 软件中经常需要使用一些外部的资源,如图片.声音.SWF或字体,虽然你也可以在软件运行的时候引入和载入,但是也可能经常需要直接将这些资源编译(Compile)到软件中,也就是直接嵌入资源(Em ...

  2. 应用安全 - 软件漏洞 - 泛微OA漏洞汇总

    SQL注入 前台SQL注入 用户名:admin' or password like 'c4ca4238a0b923820dcc509a6f75849b' and 'a'='a 密码: 1 验证页面参数 ...

  3. Linux 文件创建、插入、替换

    创建文件 touch newfile.txt 插入字符串 echo "aaa" >>/newfile.txt 替换字符串 sed -i "s/aaa/ccc/ ...

  4. [19/05/04-星期六] 正则表示式(Regular Expression)

    一.概念 语法: \D :就是不是0-9数字的其它字符: \W:与\w相反: a\d?b:表示在字符a和b之间可以有一个数字或者没有数字都可以:如:ab .a3b a\d+b:表示在字符a和b之间至少 ...

  5. 循环结构 :do-while

    循环结构 :do-while 循环四要素: 1.初始化条件 2.循环条件 3.循环体 4.迭代条件 格式: 1.初始化条件 do{ 3.循环体 4.迭代条件 }while(2.循环条件); publi ...

  6. 洛谷 P1462 通往奥格瑞玛的道路(二分答案,堆优化dijkstra)

    传送门 解题思路 首先看题目问题,求经过的所有城市中最多的一次收取的费用的最小值是多少.一看“最大值最小”就想到了二分答案. 在读一遍题目,就是二分收取的费用,然后对于每一个二分的费用,跑一边最短路, ...

  7. [luogu4768] [NOI2018] 归程 (Dijkstra+Kruskal重构树)

    [luogu4768] [NOI2018] 归程 (Dijkstra+Kruskal重构树) 题面 题面较长,这里就不贴了 分析 看到不能经过有积水的边,即不能经过边权小于一定值的边,我们想到了kru ...

  8. 运维脚本-elasticsearch数据迁移python3脚本

    elasticsearch数据迁移python3脚本 #!/usr/bin/python3 #elsearch 数据迁移脚本 #迁移工具路径 import time,os #下面命令是用到了一个go语 ...

  9. SCUT - 11 - 被钦定的选手 - 质因数分解

    https://scut.online/p/11 T了好多次,还想用mutimap暴力分解每个数的质因数.后来记录每个数的最小质因子过了. #include <bits/stdc++.h> ...

  10. linux下docker启动nginx无法访问80端口

    问题: Linux安装了docker,docker启动了一个nginx容器,通过 80 端口无法正常访问 故障排查: 1.检查 nginx 容器启动的命令或者yaml文件,查看是否有跟本机端口进行绑定 ...