Skip to content

Instantly share code, notes, and snippets.

@vikene
Created November 28, 2018 15:50
Show Gist options
  • Save vikene/3149a3e51c8237745019771fc21f1fda to your computer and use it in GitHub Desktop.
Save vikene/3149a3e51c8237745019771fc21f1fda to your computer and use it in GitHub Desktop.
Longest Increasing Subsequence
def lis_dp(X):
if not X:
return 0
memo = [1] * len(X)
for i in range(1,len(X)):
for j in range(i):
if X[j] < X[i]:
memo[i] = max(memo[i],memo[j]+1)
return max(memo)
def lis(X):
if len(X) == 0:
return 0
if len(X) == 1:
return 1
maxElement = 0
for i in range(len(X)):
maxBefore = lis(X[:i])
if X[-1] > X[i-1]:
maxElement = max(maxBefore+1, maxElement)
return maxElement
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment