【leetcode】1027. Longest Arithmetic Sequence
题目如下:
Given an array
A
of integers, return the length of the longest arithmetic subsequence inA
.Recall that a subsequence of
A
is a listA[i_1], A[i_2], ..., A[i_k]
with0 <= i_1 < i_2 < ... < i_k <= A.length - 1
, and that a sequenceB
is arithmetic ifB[i+1] - B[i]
are all the same value (for0 <= 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:
2 <= A.length <= 2000
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的更多相关文章
- 【leetcode】1218. Longest Arithmetic Subsequence of Given Difference
题目如下: Given an integer array arr and an integer difference, return the length of the longest subsequ ...
- 【LeetCode】128. Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
- LeetCode 1027. Longest Arithmetic Sequence
原题链接在这里:https://leetcode.com/problems/longest-arithmetic-sequence/ 题目: Given an array A of integers, ...
- 【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 ...
- 【leetcode】300.Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- 【LeetCode】521. Longest Uncommon Subsequence I 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】1048. Longest String Chain
题目如下: Given a list of words, each word consists of English lowercase letters. Let's say word1 is a p ...
- 【LeetCode】409. Longest Palindrome 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:字典统计次数 方法二:HashSet 方法三 ...
随机推荐
- el-date-picker用法
需求:1.默认时间是当天开始到此刻的时间 2.快捷键为今天.昨天.最近一周.最近30天.最近90天 3.不可以清空,必选项
- flex 强制垃圾回收
java和flash的垃圾回收都是一个比较热门的话题,今天我也用一个例子来测试下flash的强制垃圾回收.主要用到的而一个类是LocalConnection. 在Flash player的debug版 ...
- VMware 虚拟化编程(8) — 多线程中的 VixDiskLib
目录 目录 前文列表 多线程注意事项 多线程中的 VixDiskLib 前文列表 VMware 虚拟化编程(1) - VMDK/VDDK/VixDiskLib/VADP 概念简析 VMware 虚拟化 ...
- Markdown编辑器editor.md的使用
目录(?)[-] 一Markdown和editormd简介 二editormd的使用 1下载 2简单使用 21在自己的页面上引入相关的css和js代码如下 22在自己的页面中加上DIV 23在同页 ...
- jmeter模拟spike尖峰测
jmeter模拟spike尖峰测试 概述 尖峰测试(Spike testing)在性能测试中属于压力测试的一个子集.指的是在某一瞬间或者多个频次下用户数和压力陡然增加的场景. 为了验证我们的网站在访问 ...
- 关于jdbc和数据库连接池的关系(不是封装的关系)
你都说是数据库连接池了.那就是连接数据库用的.JDBC是java封装的对数据库的操作.当然你可以自己进一步封装.数据库连接池是JDBC使用的前提,如果连数据库连接池都没连上,JDBC的操作就谈不上了. ...
- Java设计模式——单例模式(创建型模式)
概述 单例模式保证对于每一个类加载器,一个类仅有一个实例并且提供全局的访问.其是一种对象创建型模式.对于单例模式主要适用以下几个场景: 系统只需要一个实例对象,如提供一个唯一的序列号生成器 客户调 ...
- 远程访问其他主机的Mysql(Ubuntu)
远程访问其他主机的Mysql 首先:被访问的电脑应该把防火墙关掉, sudo ufw disable 然后去etc/mysql sudo gedit my.cnf 将bind-address注解掉 由 ...
- Spring Framework基础学习
Spring Framework基础学习 Core support for dependency injection,transaction management,web applications,d ...
- MYSQL的ACID
原子性 (Atomicity) 原子性是指一个事务是一个不可分割的工作单位,其中的操作要么都做,要么都不做. 隔离性 (Isolation) 隔离性是指多个事务并发执行的时候,事务内部的操作与其他事务 ...