【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 方法三 ...
随机推荐
- nginx proxy_pass 和 proxy_redirect
proxy_pass:充当代理服务器,转发请求proxy_redirect:修改301或者302转发过程中的Location.默认值为proxy_redirect default. 例:locatio ...
- Tensorflow | 基本函数介绍 简单详细的教程。 有用, 很棒
http://blog.csdn.net/xxzhangx/article/details/54606040 Tensorflow | 基本函数介绍 2017-01-18 23:04 1404人阅读 ...
- ugui代码设置ui锚点
using UnityEngine; public enum AnchorPresets { TopLeft, TopCenter, TopRight, MiddleLeft, MiddleCente ...
- 浅谈Java反射机制 之 获取类的字节码文件 Class.forName("全路径名") 、getClass()、class
另一个篇:获取 类 的 方法 和 属性(包括构造函数) 先贴上Java反射机制的概念: AVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法: 对于任意一个对象,都能够调用它 ...
- 001/Docker入门(Mooc)
docker官网:https://www.docker.com/ 1.什么是docker 2.Docker思想 ==> [1].集装箱:保证程序完整(不缺东西,如配置文件等). [2]. ...
- Mac入门--如何使用brew安装多个PHP版本
一 安装7.1 1. 安装PHP7.1 brew install php@7.1 2. 修改配置 php-fpm.conf,一般在/usr/local/etc/php下(如果php-fpm.conf中 ...
- vue—两个数组,去重相同项
- SpringBoot 使用logback
1.添加pom引用 <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback ...
- python之cmd模块
md模块可以用来做交互式shell cmd模块是python中包含的一个公共模块,用于交互式shell和其他命令解释器等的基类,我们可以基于cmd模块自定义我们的子类,实现我们自己的交互式shell ...
- 2.golang应用目录结构和GOPATH概念
golang 语言有一个GOPATH的概念就是当前工作目录 [root@localhost golang_test]# tree . ├── bin │ └── hello ├── first.g ...