给出一个数字列表, arr (未排序),在其中找到最长的算术级数。
数组:整数
1≤ arr.size()≤10 ^ 3。 和 -10 ^ 9≤ arr [i] ≤10 ^ 9。
示例:
arr = [7,6,1,9,7,9,5,6,1,1,4,0] ------------- -输出 = [7,6,5,4]
arr = [4,4,6,7,8,13,45,67] -------------- 输出 = [4,6,8]
from itertools import combinations
def arithmeticProgression2(a):
n=len(a)
diff = ((y-x, x) for x, y in combinations(a, 2))
dic=[]
for d, n in diff:
k = []
seq=a
while n in seq:
k.append(n)
i=seq.index(n)
seq=seq[i+1:]
n += d
dic.append(k)
maxx=max([len(k) for k in dic])
for x in dic:
if len(x)==maxx:
return x
如果 arr.size()足够大。我的代码将运行4000毫秒以上。
示例:
arr = [randint(-10 ** 9,10 ** 9)for i in range(10 ** 3)]
运行时间> 4000毫秒
如何降低上述解决方案的空间复杂度?
答案 0 :(得分:1)
使代码变慢的原因之一是,您需要为每对从头开始构建系列,这是不必要的:
以下似乎使用动态编程以O(n²)更快地返回结果:
def longestprogression(data):
if len(data) < 3:
return data
maxlen = 0 # length of longest progression so far
endvalue = None # last value of longest progression
beststep = None # step of longest progression
# progressions ending in index i, keyed by their step size,
# with the progression length as value
dp = [{} for _ in range(len(data))]
# iterate all possible ending pairs of progressions
for j in range(1, len(arr)):
for i in range(j):
step = arr[j] - arr[i]
if step in dp[i]:
curlen = dp[i][step] + 1
else:
curlen = 2
dp[j][step] = curlen
if curlen > maxlen:
maxlen = curlen
endvalue = arr[j]
beststep = step
# rebuild the longest progression from the values we maintained
return list(reversed(range(endvalue, endvalue - maxlen * beststep, -beststep)))