2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2017-04-07 02:19:02 +00:00
|
|
|
Author : Mehdi ALAOUI
|
|
|
|
|
2020-05-22 06:10:11 +00:00
|
|
|
This is a pure Python implementation of Dynamic Programming solution to the longest
|
|
|
|
increasing subsequence of a given sequence.
|
2017-04-07 02:19:02 +00:00
|
|
|
|
|
|
|
The problem is :
|
2020-05-22 06:10:11 +00:00
|
|
|
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
|
2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2024-03-13 06:52:41 +00:00
|
|
|
|
2020-09-23 11:30:13 +00:00
|
|
|
from __future__ import annotations
|
2019-11-04 18:06:16 +00:00
|
|
|
|
|
|
|
|
2020-09-23 11:30:13 +00:00
|
|
|
def longest_subsequence(array: list[int]) -> list[int]: # This function is recursive
|
2019-11-04 18:06:16 +00:00
|
|
|
"""
|
|
|
|
Some examples
|
|
|
|
>>> longest_subsequence([10, 22, 9, 33, 21, 50, 41, 60, 80])
|
|
|
|
[10, 22, 33, 41, 60, 80]
|
|
|
|
>>> longest_subsequence([4, 8, 7, 5, 1, 12, 2, 3, 9])
|
|
|
|
[1, 2, 3, 9]
|
|
|
|
>>> longest_subsequence([9, 8, 7, 6, 5, 7])
|
|
|
|
[8]
|
|
|
|
>>> longest_subsequence([1, 1, 1])
|
|
|
|
[1, 1, 1]
|
2020-05-22 06:10:11 +00:00
|
|
|
>>> longest_subsequence([])
|
|
|
|
[]
|
2019-11-04 18:06:16 +00:00
|
|
|
"""
|
|
|
|
array_length = len(array)
|
2020-05-22 06:10:11 +00:00
|
|
|
# If the array contains only one element, we return it (it's the stop condition of
|
|
|
|
# recursion)
|
|
|
|
if array_length <= 1:
|
2019-11-04 18:06:16 +00:00
|
|
|
return array
|
2019-10-05 05:14:13 +00:00
|
|
|
# Else
|
2019-11-04 18:06:16 +00:00
|
|
|
pivot = array[0]
|
2022-10-12 22:54:20 +00:00
|
|
|
is_found = False
|
2019-10-05 05:14:13 +00:00
|
|
|
i = 1
|
2021-09-03 09:49:23 +00:00
|
|
|
longest_subseq: list[int] = []
|
2022-10-12 22:54:20 +00:00
|
|
|
while not is_found and i < array_length:
|
2019-11-04 18:06:16 +00:00
|
|
|
if array[i] < pivot:
|
2022-10-12 22:54:20 +00:00
|
|
|
is_found = True
|
2019-11-04 18:06:16 +00:00
|
|
|
temp_array = [element for element in array[i:] if element >= array[i]]
|
|
|
|
temp_array = longest_subsequence(temp_array)
|
|
|
|
if len(temp_array) > len(longest_subseq):
|
|
|
|
longest_subseq = temp_array
|
2019-10-05 05:14:13 +00:00
|
|
|
else:
|
|
|
|
i += 1
|
|
|
|
|
2019-11-04 18:06:16 +00:00
|
|
|
temp_array = [element for element in array[1:] if element >= pivot]
|
2023-03-01 16:23:33 +00:00
|
|
|
temp_array = [pivot, *longest_subsequence(temp_array)]
|
2019-11-04 18:06:16 +00:00
|
|
|
if len(temp_array) > len(longest_subseq):
|
|
|
|
return temp_array
|
2019-10-05 05:14:13 +00:00
|
|
|
else:
|
2019-11-04 18:06:16 +00:00
|
|
|
return longest_subseq
|
2019-11-14 18:59:43 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2019-11-04 18:06:16 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
import doctest
|
2019-11-14 18:59:43 +00:00
|
|
|
|
2019-11-04 18:06:16 +00:00
|
|
|
doctest.testmod()
|