-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Script Output: The entire longest increasing subsequence instead of it's length #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,41 @@ | ||
""" | ||
The Longest Increasing Subsequence (LIS) problem is to find the length of the longest subsequence of a given sequence such that all elements of the subsequence are sorted in increasing order. For example, the length of LIS for {10, 22, 9, 33, 21, 50, 41, 60, 80} is 6 | ||
""" | ||
def LIS(arr): | ||
n= len(arr) | ||
lis = [1]*n | ||
|
||
for i in range(1, n): | ||
for j in range(0, i): | ||
if arr[i] > arr[j] and lis[i] <= lis[j]: | ||
lis[i] = lis[j] + 1 | ||
return max(lis) | ||
''' | ||
Author : Mehdi ALAOUI | ||
|
||
This is a pure Python implementation of Dynamic Programming solution to the longest increasing subsequence of a given sequence. | ||
|
||
The problem is : | ||
Given an ARRAY, to find the longest and increasing sub ARRAY in that given ARRAY and return it. | ||
Example: [10, 22, 9, 33, 21, 50, 41, 60, 80] as input will return [10, 22, 33, 41, 60, 80] as output | ||
''' | ||
|
||
def longestSub(ARRAY): #This function is recursive | ||
|
||
ARRAY_LENGTH = len(ARRAY) | ||
if(ARRAY_LENGTH <= 1): #If the array contains only one element, we return it (it's the stop condition of recursion) | ||
return ARRAY | ||
#Else | ||
PIVOT=ARRAY[0] | ||
isFound=False | ||
i=1 | ||
LONGEST_SUB=[] | ||
while(not isFound and i<ARRAY_LENGTH): | ||
if (ARRAY[i] < PIVOT): | ||
isFound=True | ||
TEMPORARY_ARRAY = [ element for element in ARRAY[i:] if element >= ARRAY[i] ] | ||
TEMPORARY_ARRAY = longestSub(TEMPORARY_ARRAY) | ||
if ( len(TEMPORARY_ARRAY) > len(LONGEST_SUB) ): | ||
LONGEST_SUB = TEMPORARY_ARRAY | ||
else: | ||
i+=1 | ||
|
||
TEMPORARY_ARRAY = [ element for element in ARRAY[1:] if element >= PIVOT ] | ||
TEMPORARY_ARRAY = [PIVOT] + longestSub(TEMPORARY_ARRAY) | ||
if ( len(TEMPORARY_ARRAY) > len(LONGEST_SUB) ): | ||
return TEMPORARY_ARRAY | ||
else: | ||
return LONGEST_SUB | ||
|
||
#Some examples | ||
|
||
print(longestSub([4,8,7,5,1,12,2,3,9])) | ||
print(longestSub([9,8,7,6,5,7])) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please mention time and space complexity of the code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just finished some optimizations on complexity, it was, on the worst case O(2^(n-1)), but now, I think that in the worst case, it's O(nlog(n)), if I'm wrong, please correct me.