Algorithm 最长递增子序列2d

Algorithm 最长递增子序列2d,algorithm,subsequence,Algorithm,Subsequence,我有n个按固定顺序排列的m个整数数组。我需要找到一个最长的递增子序列,这样子序列中的每个元素都恰好属于一个数组。我能比O(n2)做得更好吗 与@svs一致,这不可能在不到O(m*n)的时间内实现。然而,在实践中,一旦知道不可能在数组中找到更长的子序列,就可以通过终止对数组的迭代来减少平均最坏时间 平凡循环: maxList = [] for arr in arrays: last = arr[0] - 1 tempList = [] for element in arr:

我有n个按固定顺序排列的m个整数数组。我需要找到一个最长的递增子序列,这样子序列中的每个元素都恰好属于一个数组。我能比O(n2)做得更好吗

与@svs一致,这不可能在不到O(m*n)的时间内实现。然而,在实践中,一旦知道不可能在数组中找到更长的子序列,就可以通过终止对数组的迭代来减少平均最坏时间

平凡循环:

maxList = []
for arr in arrays:
    last = arr[0] - 1
    tempList = []
    for element in arr:
        if element > last:
            tempList.append(element)
            if len(tempList) > len(maxList):
                    maxList = tempList

        else:
            tempList = [element]
        last = element

return (maxList, iters)
maxList = []
for arr in arrays:
    if len(maxList) == len(arr):
        break

    last = arr[0] - 1
    tempList = []
    for (index, element) in enumerate(arr):
        if element > last:
            tempList.append(element)
            if len(tempList) > len(maxList):
                    maxList = tempList[:]
        else:
            tempList = [element]

        # if continuing looking down the array could not result in a longer
        # increasing sequence
        if (len(tempList) + (len(arr) - (index + 1)) <= len(maxList)):
            break

        last = element

return (maxList, iters)
忽略冗余循环迭代:

maxList = []
for arr in arrays:
    last = arr[0] - 1
    tempList = []
    for element in arr:
        if element > last:
            tempList.append(element)
            if len(tempList) > len(maxList):
                    maxList = tempList

        else:
            tempList = [element]
        last = element

return (maxList, iters)
maxList = []
for arr in arrays:
    if len(maxList) == len(arr):
        break

    last = arr[0] - 1
    tempList = []
    for (index, element) in enumerate(arr):
        if element > last:
            tempList.append(element)
            if len(tempList) > len(maxList):
                    maxList = tempList[:]
        else:
            tempList = [element]

        # if continuing looking down the array could not result in a longer
        # increasing sequence
        if (len(tempList) + (len(arr) - (index + 1)) <= len(maxList)):
            break

        last = element

return (maxList, iters)
maxList=[]
对于阵列中的arr:
如果len(maxList)=len(arr):
打破
last=arr[0]-1
圣殿骑士=[]
对于枚举(arr)中的(索引、元素):
如果元素>最后一个:
templast.append(元素)
如果len(templast)>len(maxList):
maxList=tempList[:]
其他:
圣堂武士=[元素]
#如果继续向下查看,则阵列不会导致更长的时间
#递增序列

如果(len(templast)+(len(arr)-(index+1))是的,它可以通过动态编程和记忆来完成……复杂性将是O(n Log(base2)n)别名O(nLogn)。为了证明这一点,我在递归的每次迭代中都使用了一个外部静态复杂性变量(名为complexity)和增量,以表明复杂性将是O(nLogn)-


关键是,在每次递归中,我们都会计算第i个索引处的LIS并将其存储在memo map中。此外,当我们在(i+1)次迭代时,由于memo map的可用性,我们不需要重新计算(递归)整个0到第i个索引,它将复杂性从指数级降低到nlogn级。

假设
m
可以与
n
一样大,并且您必须读取至少
Lambda(n^2)
所需的所有元素。换句话说,您不能。