Created
March 25, 2016 07:42
-
-
Save rishi93/d1e57f09a3b8c249cb0f to your computer and use it in GitHub Desktop.
Longest Increasing Subsequence
This file contains hidden or 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(arr,n): | |
dp = [1 for x in range(0,n)] | |
for i in range(1,n): | |
for j in range(0,i): | |
if dp[j] >= dp[i] and arr[j] < arr[i]: | |
dp[i]+=1 | |
max_value = max(dp) | |
result = [] | |
for i in range(n-1,-1,-1): | |
if dp[i] == max_value: | |
result.append(arr[i]) | |
max_value -= 1 | |
result.reverse() | |
return result | |
n = int(input()) | |
arr = [int(x) for x in input().split()] | |
print(lis(arr,n)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment