Created
November 28, 2018 15:50
-
-
Save vikene/3149a3e51c8237745019771fc21f1fda to your computer and use it in GitHub Desktop.
Longest Increasing Subsequence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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