Skip to content

Instantly share code, notes, and snippets.

@rishi93
Created March 25, 2016 07:42
Show Gist options
  • Save rishi93/d1e57f09a3b8c249cb0f to your computer and use it in GitHub Desktop.
Save rishi93/d1e57f09a3b8c249cb0f to your computer and use it in GitHub Desktop.
Longest Increasing Subsequence
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