Skip to content

Instantly share code, notes, and snippets.

@st0le
Created June 8, 2013 08:01
Show Gist options
  • Save st0le/5734468 to your computer and use it in GitHub Desktop.
Save st0le/5734468 to your computer and use it in GitHub Desktop.
Longest Increasing Sequence DFS
def longest_increasing_subsequence_dfs(lst):
lis_seq = []
sz = len(lst)
def lis(index,seq):
if len(seq) + (sz - index) < len(lis_seq): return #prune the rest.
for i in xrange(index,sz):
if seq[-1] < lst[i]:
seq.append(lst[i])
lis(i+1,seq)
seq.pop()
else:
if len(seq) > len(lis_seq):
l = lis_seq
l[:] = seq
for i in xrange(sz):
lis(i + 1,[lst[i]])
return lis_seq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment