|
15 | 15 |
|
16 | 16 | from __future__ import annotations
|
17 | 17 |
|
| 18 | +import copy |
| 19 | + |
18 | 20 |
|
19 | 21 | def longest_subsequence(array: list[int]) -> list[int]: # This function is recursive
|
20 | 22 | """
|
21 | 23 | Some examples
|
22 | 24 |
|
23 | 25 | >>> longest_subsequence([10, 22, 9, 33, 21, 50, 41, 60, 80])
|
24 |
| - [10, 22, 33, 41, 60, 80] |
| 26 | + [10, 22, 33, 50, 60, 80] |
25 | 27 | >>> longest_subsequence([4, 8, 7, 5, 1, 12, 2, 3, 9])
|
26 | 28 | [1, 2, 3, 9]
|
27 | 29 | >>> longest_subsequence([9, 8, 7, 6, 5, 7])
|
28 |
| - [8] |
| 30 | + [7, 7] |
29 | 31 | >>> longest_subsequence([1, 1, 1])
|
30 | 32 | [1, 1, 1]
|
31 | 33 | >>> longest_subsequence([])
|
32 | 34 | []
|
33 | 35 | """
|
34 |
| - array_length = len(array) |
35 |
| - # If the array contains only one element, we return it (it's the stop condition of |
36 |
| - # recursion) |
37 |
| - if array_length <= 1: |
38 |
| - return array |
39 |
| - # Else |
40 |
| - pivot = array[0] |
41 |
| - is_found = False |
42 |
| - i = 1 |
43 |
| - longest_subseq: list[int] = [] |
44 |
| - while not is_found and i < array_length: |
45 |
| - if array[i] < pivot: |
46 |
| - is_found = True |
47 |
| - temp_array = [element for element in array[i:] if element >= array[i]] |
48 |
| - temp_array = longest_subsequence(temp_array) |
49 |
| - if len(temp_array) > len(longest_subseq): |
50 |
| - longest_subseq = temp_array |
51 |
| - else: |
52 |
| - i += 1 |
53 |
| - |
54 |
| - temp_array = [element for element in array[1:] if element >= pivot] |
55 |
| - temp_array = [pivot, *longest_subsequence(temp_array)] |
56 |
| - if len(temp_array) > len(longest_subseq): |
57 |
| - return temp_array |
58 |
| - else: |
59 |
| - return longest_subseq |
| 36 | + n = len(array) |
| 37 | + # The longest increasing subsequence ending at array[i] |
| 38 | + longest_increasing_subsequence = [] |
| 39 | + for i in range(n): |
| 40 | + longest_increasing_subsequence.append([array[i]]) |
| 41 | + |
| 42 | + for i in range(1, n): |
| 43 | + for prev in range(i): |
| 44 | + # If array[prev] is less than array[i], then |
| 45 | + # longest_increasing_subsequence[prev] + array[i] |
| 46 | + # is a valid increasing subsequence |
| 47 | + |
| 48 | + # longest_increasing_subsequence[i] is only set to |
| 49 | + # longest_increasing_subsequence[prev] + array[i] if the length is longer. |
| 50 | + |
| 51 | + if array[prev] <= array[i] and len( |
| 52 | + longest_increasing_subsequence[prev] |
| 53 | + ) + 1 > len(longest_increasing_subsequence[i]): |
| 54 | + longest_increasing_subsequence[i] = copy.copy( |
| 55 | + longest_increasing_subsequence[prev] |
| 56 | + ) |
| 57 | + longest_increasing_subsequence[i].append(array[i]) |
| 58 | + |
| 59 | + result: list[int] = [] |
| 60 | + for i in range(n): |
| 61 | + if len(longest_increasing_subsequence[i]) > len(result): |
| 62 | + result = longest_increasing_subsequence[i] |
| 63 | + |
| 64 | + return result |
60 | 65 |
|
61 | 66 |
|
62 | 67 | if __name__ == "__main__":
|
|
0 commit comments