Advertisement
mhrabbi

Longest Increasing Subsequence Dynamic programming

Aug 21st, 2020
833
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.82 KB | None | 0 0
  1. # Longest Increasing Subsequence Dynamic programming
  2. # R@1313I
  3.  
  4.  
  5. # Function that returns the Number of Longest Increasing Subsequence
  6. def lonIncreSub(arr):
  7.     n = len(arr)
  8.  
  9.     # Set All the value to 1
  10.     lis = [1] * n
  11.  
  12.     # Compare the current value with previous all the value and increase the lis count if current v is greater than previous v
  13.     for i in range(1, n):
  14.         for j in range(0, i):
  15.             if arr[i] > arr[j] and lis[i] < lis[j] + 1:
  16.                 lis[i] = lis[j] + 1
  17.  
  18.  
  19.     # Set the max number to 0
  20.     maximum = 0
  21.  
  22.     # FInd the maximum of all LIS values
  23.     for i in range(n):
  24.         maximum = max(maximum, lis[i])
  25.  
  26.     return maximum
  27.  
  28.  
  29. arr = list(map(int, input("Enter Some Subsequence Number: ").split()))
  30. print("Longest Subsequence Number in the list is: ", lonIncreSub(arr))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement