如何解决生成所有增加的子序列
给定一个整数数组,我们如何生成所有递增的子序列,使它们都具有相同的长度?
示例:给出此列表
l = [1,2,4,5,3,6]
答案应该是,如果我们考虑长度为4的子序列:
[1,6]
[1,5]
[1,6]
[2,6]
from itertools import combinations
# the second item of the tuple is the position of the element in the array
zeta = [(1,1),(2,2),(4,3),(5,4),(3,5),(6,6)]
comb = combinations(sorted(zeta,key=lambda x: x[0]),4)
def verif(x):
l = []
for k in x:
l.append(k[1])
for i in range(len(l)-1):
if l[i+1]-l[i] < 0:
return 0
return 1
for i in comb:
if verif(list(i)):
print(i)
我想要一种更好的方法,例如动态编程解决方案,因为显然,对于较大的整数列表,我的解决方案非常慢。 LIS problem在这种情况下有帮助吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。