From 74e269b5bf3eeea9c1eb30110be8e407ba2a392b Mon Sep 17 00:00:00 2001 From: Sanjay Muthu Date: Sun, 12 Jan 2025 17:27:47 +0530 Subject: [PATCH 1/8] Fix #12510 --- .../longest_increasing_subsequence.py | 27 +++++++------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/dynamic_programming/longest_increasing_subsequence.py b/dynamic_programming/longest_increasing_subsequence.py index d839757f6da5..ea2cdc2b10e2 100644 --- a/dynamic_programming/longest_increasing_subsequence.py +++ b/dynamic_programming/longest_increasing_subsequence.py @@ -25,7 +25,7 @@ def longest_subsequence(array: list[int]) -> list[int]: # This function is recu >>> longest_subsequence([4, 8, 7, 5, 1, 12, 2, 3, 9]) [1, 2, 3, 9] >>> longest_subsequence([9, 8, 7, 6, 5, 7]) - [8] + [5, 7] >>> longest_subsequence([1, 1, 1]) [1, 1, 1] >>> longest_subsequence([]) @@ -38,25 +38,16 @@ def longest_subsequence(array: list[int]) -> list[int]: # This function is recu return array # Else pivot = array[0] - is_found = False - i = 1 - longest_subseq: list[int] = [] - while not is_found and i < array_length: - if array[i] < pivot: - is_found = True - 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 - else: - i += 1 - temp_array = [element for element in array[1:] if element >= pivot] - temp_array = [pivot, *longest_subsequence(temp_array)] - if len(temp_array) > len(longest_subseq): - return temp_array + #Either the subsequence contains the pivot or it doesnt + #The sub_sequence which is longer will be returned + without_pivot = longest_subsequence(array[1:]) + with_pivot = [element for element in array[1:] if element >= pivot] + with_pivot = [pivot, *longest_subsequence(with_pivot)] + if len(with_pivot) > len(without_pivot): + return with_pivot else: - return longest_subseq + return without_pivot if __name__ == "__main__": From dd3c159a73cbd7a9b924e9fbf45df5068108cb47 Mon Sep 17 00:00:00 2001 From: Sanjay Muthu Date: Sun, 12 Jan 2025 17:32:22 +0530 Subject: [PATCH 2/8] Added the doctest mentioned in the issue --- dynamic_programming/longest_increasing_subsequence.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dynamic_programming/longest_increasing_subsequence.py b/dynamic_programming/longest_increasing_subsequence.py index ea2cdc2b10e2..da8f65140890 100644 --- a/dynamic_programming/longest_increasing_subsequence.py +++ b/dynamic_programming/longest_increasing_subsequence.py @@ -24,6 +24,8 @@ def longest_subsequence(array: list[int]) -> list[int]: # This function is recu [10, 22, 33, 41, 60, 80] >>> longest_subsequence([4, 8, 7, 5, 1, 12, 2, 3, 9]) [1, 2, 3, 9] + >>> longest_subsequence([28, 26, 12, 23, 35, 39]) + [12, 23, 35, 39] >>> longest_subsequence([9, 8, 7, 6, 5, 7]) [5, 7] >>> longest_subsequence([1, 1, 1]) From 8cb965f1eab0887550d09c3b7103ca35f0a9a379 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 12 Jan 2025 12:05:38 +0000 Subject: [PATCH 3/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/longest_increasing_subsequence.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dynamic_programming/longest_increasing_subsequence.py b/dynamic_programming/longest_increasing_subsequence.py index da8f65140890..c834f9dd61bd 100644 --- a/dynamic_programming/longest_increasing_subsequence.py +++ b/dynamic_programming/longest_increasing_subsequence.py @@ -41,8 +41,8 @@ def longest_subsequence(array: list[int]) -> list[int]: # This function is recu # Else pivot = array[0] - #Either the subsequence contains the pivot or it doesnt - #The sub_sequence which is longer will be returned + # Either the subsequence contains the pivot or it doesnt + # The sub_sequence which is longer will be returned without_pivot = longest_subsequence(array[1:]) with_pivot = [element for element in array[1:] if element >= pivot] with_pivot = [pivot, *longest_subsequence(with_pivot)] From 40910bf6a9e3a04f515ac05040d85c38f6896e2d Mon Sep 17 00:00:00 2001 From: Sanjay Muthu Date: Sun, 12 Jan 2025 17:39:47 +0530 Subject: [PATCH 4/8] Fixed Grammer Mistake --- dynamic_programming/longest_increasing_subsequence.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dynamic_programming/longest_increasing_subsequence.py b/dynamic_programming/longest_increasing_subsequence.py index c834f9dd61bd..fbaae5fd8bfa 100644 --- a/dynamic_programming/longest_increasing_subsequence.py +++ b/dynamic_programming/longest_increasing_subsequence.py @@ -41,8 +41,9 @@ def longest_subsequence(array: list[int]) -> list[int]: # This function is recu # Else pivot = array[0] - # Either the subsequence contains the pivot or it doesnt - # The sub_sequence which is longer will be returned + # Either the subsequence contains the pivot or it does not, + # The longest subsequence is calculated in both cases and + # The longer subsequence is returned without_pivot = longest_subsequence(array[1:]) with_pivot = [element for element in array[1:] if element >= pivot] with_pivot = [pivot, *longest_subsequence(with_pivot)] From aebba8cec998ec02b23f577b0ea7fb315920ae3c Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Mon, 13 Jan 2025 02:21:35 +0300 Subject: [PATCH 5/8] Update longest_increasing_subsequence.py --- dynamic_programming/longest_increasing_subsequence.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dynamic_programming/longest_increasing_subsequence.py b/dynamic_programming/longest_increasing_subsequence.py index fbaae5fd8bfa..e9da676470f4 100644 --- a/dynamic_programming/longest_increasing_subsequence.py +++ b/dynamic_programming/longest_increasing_subsequence.py @@ -32,6 +32,8 @@ def longest_subsequence(array: list[int]) -> list[int]: # This function is recu [1, 1, 1] >>> longest_subsequence([]) [] + >>> longest_subsequence([28, 26, 12, 23, 35, 39]) + [12, 23, 35, 39] """ array_length = len(array) # If the array contains only one element, we return it (it's the stop condition of From 1a73d2d2d996cd3f52d083fc9700154982337f3d Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Mon, 13 Jan 2025 02:25:16 +0300 Subject: [PATCH 6/8] Update longest_increasing_subsequence.py --- .../longest_increasing_subsequence.py | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/dynamic_programming/longest_increasing_subsequence.py b/dynamic_programming/longest_increasing_subsequence.py index e9da676470f4..1e1d9e8a75d4 100644 --- a/dynamic_programming/longest_increasing_subsequence.py +++ b/dynamic_programming/longest_increasing_subsequence.py @@ -42,17 +42,25 @@ def longest_subsequence(array: list[int]) -> list[int]: # This function is recu return array # Else pivot = array[0] + is_found = False + i = 1 + longest_subseq: list[int] = [] + while not is_found and i < array_length: + if array[i] < pivot: + is_found = True + temp_array = [element for element in array[i:]] + temp_array = longest_subsequence(temp_array) + if len(temp_array) > len(longest_subseq): + longest_subseq = temp_array + else: + i += 1 - # Either the subsequence contains the pivot or it does not, - # The longest subsequence is calculated in both cases and - # The longer subsequence is returned - without_pivot = longest_subsequence(array[1:]) - with_pivot = [element for element in array[1:] if element >= pivot] - with_pivot = [pivot, *longest_subsequence(with_pivot)] - if len(with_pivot) > len(without_pivot): - return with_pivot + temp_array = [element for element in array[1:] if element >= pivot] + temp_array = [pivot, *longest_subsequence(temp_array)] + if len(temp_array) > len(longest_subseq): + return temp_array else: - return without_pivot + return longest_subseq if __name__ == "__main__": From f0447ce660fac9a35025c5190f3fd83a1197ee88 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Mon, 13 Jan 2025 02:26:01 +0300 Subject: [PATCH 7/8] Update longest_increasing_subsequence.py --- dynamic_programming/longest_increasing_subsequence.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/dynamic_programming/longest_increasing_subsequence.py b/dynamic_programming/longest_increasing_subsequence.py index 1e1d9e8a75d4..2b5475d30414 100644 --- a/dynamic_programming/longest_increasing_subsequence.py +++ b/dynamic_programming/longest_increasing_subsequence.py @@ -32,8 +32,6 @@ def longest_subsequence(array: list[int]) -> list[int]: # This function is recu [1, 1, 1] >>> longest_subsequence([]) [] - >>> longest_subsequence([28, 26, 12, 23, 35, 39]) - [12, 23, 35, 39] """ array_length = len(array) # If the array contains only one element, we return it (it's the stop condition of From 444220d774e21b31db8b207c4fd2a445f2a9da55 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Mon, 13 Jan 2025 02:27:16 +0300 Subject: [PATCH 8/8] Update longest_increasing_subsequence.py --- dynamic_programming/longest_increasing_subsequence.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/longest_increasing_subsequence.py b/dynamic_programming/longest_increasing_subsequence.py index 2b5475d30414..1863a882c41e 100644 --- a/dynamic_programming/longest_increasing_subsequence.py +++ b/dynamic_programming/longest_increasing_subsequence.py @@ -46,7 +46,7 @@ def longest_subsequence(array: list[int]) -> list[int]: # This function is recu while not is_found and i < array_length: if array[i] < pivot: is_found = True - temp_array = [element for element in array[i:]] + temp_array = array[i:] temp_array = longest_subsequence(temp_array) if len(temp_array) > len(longest_subseq): longest_subseq = temp_array