Good algorithm for combining items from N lists into one with balanced distribution?

前端 未结 7 975
北荒
北荒 2020-12-05 20:54

Let\'s say I have the three following lists

A1
A2
A3

B1
B2

C1
C2
C3
C4
C5

I\'d like to combine them into a si

相关标签:
7条回答
  • 2020-12-05 21:53

    A quick suggestion, in python-ish pseudocode:

    merge = list()
    lists = list(list_a, list_b, list_c)
    lists.sort_by(length, descending)
    
    while lists is not empty:
        l = lists.remove_first()
        merge.append(l.remove_first())
        if l is not empty:
            next = lists.remove_first()
            lists.append(l)
            lists.sort_by(length, descending)
            lists.prepend(next)
    

    This should distribute elements from shorter lists more evenly than the other suggestions here.

    0 讨论(0)
提交回复
热议问题