python代码
正向
def findMaxSubSequence(self):
list= self.sequence
n = len(list)
m = [1] * n #m[x]中存着list中0-x项中递增子序列的长度
for x in range(1,n):
for y in range (0,x): #list从前往后计算
if list[x] > list[y] and m[x] <= m[y]: # 计算m的x项 ,更新m的0~x-1项
m[x] += 1
subseq = []
max_value = max(m)
for i in range(n-1,-1,-1): #获取0-x项递增子序列
if m[i] == max_value:
subseq.append(list[i])
max_value -= 1
subseq.reverse()
return subseq
反向
def findMaxSubSequence(self):
list= self.sequence
n = len(list)
m = [0] * n #m[x]中存着list中x~n-1项中递增子序列的长度
for x in range(n-2, -1, -1):
for y in range(n-1, x, -1): #从list后往前计算
if list[x] < list[y] and m[x] <= m[y]: #计算m的x项,更新m的x-1~n-1项
m[x] += 1
max_value = max(m)
subseq = []
for i in range(n): # 获取x~n-1项递增子序列
if m[i] == max_value:
subseq.append(list[i])
max_value -= 1
return subseq