Skip to content

Instantly share code, notes, and snippets.

@dionyziz
Created August 14, 2019 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dionyziz/38c4759ecd54d5c8525aed671f109268 to your computer and use it in GitHub Desktop.
Save dionyziz/38c4759ecd54d5c8525aed671f109268 to your computer and use it in GitHub Desktop.
def longest_increasing_subsequence(sequence):
best_length = -1
for bitmask in range(2**len(sequence)):
subsequence = []
for bit_idx in range(len(sequence)):
if bitmask & (1 << bit_idx):
subsequence.append(sequence[bit_idx])
if not sorted(subsequence) == subsequence:
continue
if len(subsequence) > best_length:
best_length = len(subsequence)
best = subsequence
return best
print(longest_increasing_subsequence([1, 5, 2, 3, 9, 10, 3, 3, 5]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment