In python, how does one efficiently find the largest consecutive set of numbers in a list that are not necessarily adjacent?

前端 未结 10 755

For instance, if I have a list

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

This algorithm should return [1,2,3,4,5,6,7,8,9,10,11].

To cl

10条回答
  •  难免孤独
    2020-12-31 11:57

    Here is a simple one-pass O(n) solution:

    s = [1,4,2,3,5,4,5,6,7,8,1,3,4,5,9,10,11,42]
    maxrun = -1
    rl = {}
    for x in s:
        run = rl[x] = rl.get(x-1, 0) + 1
        print x-run+1, 'to', x
        if run > maxrun:
            maxend, maxrun = x, run
    print range(maxend-maxrun+1, maxend+1)
    

    The logic may be a little more self-evident if you think in terms of ranges instead of individual variables for the endpoint and run length:

    rl = {}
    best_range = xrange(0)
    for x in s:
        run = rl[x] = rl.get(x-1, 0) + 1
        r = xrange(x-run+1, x+1)
        if len(r) > len(best_range):
            best_range = r
    print list(best_range)
    

提交回复
热议问题