fork download
  1. from bisect import bisect_right
  2. from operator import itemgetter
  3.  
  4.  
  5. def out_of_sequence(seq, key = None):
  6. if key is None: key = lambda x: x
  7.  
  8. lastoflength = [0] # end position of subsequence with given length
  9. predecessor = [None] # penultimate element of l.i.s. ending at given position
  10.  
  11. for i in range(1, len(seq)):
  12. # find length j of subsequence that seq[i] can extend
  13. j = bisect_right([key(seq[k]) for k in lastoflength], key(seq[i]))
  14. # update old subsequence or extend the longest
  15. try: lastoflength[j] = i
  16. except: lastoflength.append(i)
  17. # record element preceding seq[i] in the subsequence for backtracking
  18. predecessor.append(lastoflength[j-1] if j > 0 else None)
  19.  
  20. indices = set()
  21. i = lastoflength[-1]
  22. while i is not None:
  23. indices.add(i)
  24. i = predecessor[i]
  25.  
  26. return [e for i, e in enumerate(seq) if i not in indices]
  27.  
  28. print(out_of_sequence([991, 992, 993, 1, 2, 3, 4, 5, 994, 995, 996, 997, 998, 999]))
  29.  
Success #stdin #stdout 0.02s 9936KB
stdin
Standard input is empty
stdout
[991, 992, 993]