如何合并k个排序的数组,解决方案无法重复使用!

时间:2018-11-23 02:03:58

标签: python python-3.x sorting heap heapq

给出k个大小为n的排序数组,将它们合并并打印排序后的输出。

我遵循的算法是

    每个数组的
  • 迭代
    • 在k个数组中选择第ith个索引
    • insert() in minheap
    • delMin()并附加结果数组。

from heapq import heappop, heappush

def merge_k_arrays(list_of_lists):
    result = [] #len(list_of_lists[0])*len(list_of_lists)
    minHeap= []
    n, k=0,0

    print(list_of_lists)
    while n < len(list_of_lists[0]):
        if n ==0:# initial k size heap ready
            while k < len(list_of_lists):
                element= list_of_lists[k][n]
                heappush(minHeap ,element )
                k+=1
            result.append(heappop(minHeap))
        else: # one at a time.
            k =0
            while k < len(list_of_lists):
                element = list_of_lists[k][n]
                heappush(minHeap , element)
                result.append(heappop(minHeap))
                k+=1
        n += 1

    # add the left overs in the heap
    while minHeap:
        result.append(heappop(minHeap))

    return result

输入:

input = [   [1, 3, 5, 7],
            [2, 4, 6, 8],
            [0, 9, 10, 11],

        ] 

输出:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

输入:

input = [   [1, 3, 5, 7],
            [2, 4, 6, 8],
            [3, 3, 3, 3],
            [7, 7, 7,7]
        ]

输出:

[0, 1, 2, 3, 3, 3, 4, 5, 6, 3, 7, 7, 7, 7, 3, 7, 8, 9, 10, 11]

有人能帮助我知道我的算法中缺少什么,以便也将第二个输入中的重复数组合并吗?

1 个答案:

答案 0 :(得分:-1)

要修复代码,请在第二个嵌套的while循环中将result.append(heappop(minHeap))移动到嵌套的while循环的外部,就像在您的第一个嵌套的while循环中一样。这将使您的代码正常工作。

        else: # one at a time.
        k =0
        while k < len(list_of_lists):
            element = list_of_lists[k][n]
            heappush(minHeap , element)

            k+=1
        result.append(heappop(minHeap))
    n += 1

如果您有任何空间限制,这仍然是有问题的,因为您几乎要将整个输入都添加到堆中。如果空间不是问题,那么可以使用一种更简洁的方法编写解决方案:

def merge(A):
    result = []
    heap = [e for row in A for e in row]
    heapify(heap)
    for i in range(len(heap)):
        result.append(heappop(heap))
    return result

否则,您将需要使用一种更智能的解决方案,该解决方案仅允许堆总共包含k个元素,每个列表中只有一个元素,并且添加到每个步骤的新元素应该来自该元素的原始列表,刚刚弹出。