Merge sorted lists in python

前端 未结 9 1003
花落未央
花落未央 2020-12-19 00:50

I have a bunch of sorted lists of objects, and a comparison function

class Obj :
    def __init__(p) :
        self.points = p
def cmp(a, b) :
    return a.p         


        
9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-19 01:11

    Instead of using a list, you can use a [heap](http://en.wikipedia.org/wiki/Heap_(data_structure).

    The insertion is O(log(n)), so merging a, b and c will be O(n log(n))

    In Python, you can use the heapq module.

提交回复
热议问题