From fe57ec3c8cebeed5cfa116079f78f8dd4b864cc2 Mon Sep 17 00:00:00 2001 From: Sanjay Muthu Date: Tue, 14 Jan 2025 13:02:34 +0530 Subject: [PATCH 1/3] Create longest_increasing_subsequence_iterative.py --- ...ongest_increasing_subsequence_iterative.py | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 dynamic_programming/longest_increasing_subsequence_iterative.py diff --git a/dynamic_programming/longest_increasing_subsequence_iterative.py b/dynamic_programming/longest_increasing_subsequence_iterative.py new file mode 100644 index 000000000000..53b5cfa59d3d --- /dev/null +++ b/dynamic_programming/longest_increasing_subsequence_iterative.py @@ -0,0 +1,72 @@ +""" +Author : Sanjay Muthu + +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, 50, 60, 80]`` as output +""" + +from __future__ import annotations + +import copy + + +def longest_subsequence(array: list[int]) -> list[int]: # This function is recursive + """ + Some examples + + >>> longest_subsequence([10, 22, 9, 33, 21, 50, 41, 60, 80]) + [10, 22, 33, 50, 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]) + [7, 7] + >>> longest_subsequence([28, 26, 12, 23, 35, 39]) + [12, 23, 35, 39] + >>> longest_subsequence([1, 1, 1]) + [1, 1, 1] + >>> longest_subsequence([]) + [] + """ + n = len(array) + # The longest increasing subsequence ending at array[i] + longest_increasing_subsequence = [] + for i in range(n): + longest_increasing_subsequence.append([array[i]]) + + for i in range(1, n): + for prev in range(i): + # If array[prev] is less than array[i], then + # longest_increasing_subsequence[prev] + array[i] + # is a valid increasing subsequence + + # longest_increasing_subsequence[i] is only set to + # longest_increasing_subsequence[prev] + array[i] if the length is longer. + + if array[prev] <= array[i] and len( + longest_increasing_subsequence[prev] + ) + 1 > len(longest_increasing_subsequence[i]): + longest_increasing_subsequence[i] = copy.copy( + longest_increasing_subsequence[prev] + ) + longest_increasing_subsequence[i].append(array[i]) + + result: list[int] = [] + for i in range(n): + if len(longest_increasing_subsequence[i]) > len(result): + result = longest_increasing_subsequence[i] + + return result + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 05b25517a153308d4d344f7b2650a8a2800c3271 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Tue, 14 Jan 2025 23:40:45 +0300 Subject: [PATCH 2/3] Update longest_increasing_subsequence_iterative.py --- dynamic_programming/longest_increasing_subsequence_iterative.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/longest_increasing_subsequence_iterative.py b/dynamic_programming/longest_increasing_subsequence_iterative.py index 53b5cfa59d3d..9a25d517cce5 100644 --- a/dynamic_programming/longest_increasing_subsequence_iterative.py +++ b/dynamic_programming/longest_increasing_subsequence_iterative.py @@ -18,7 +18,7 @@ import copy -def longest_subsequence(array: list[int]) -> list[int]: # This function is recursive +def longest_subsequence(array: list[int]) -> list[int]: """ Some examples From 60f8fc2b86ffe4d7fae4faa2e6c941b873cd8189 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Tue, 14 Jan 2025 23:44:21 +0300 Subject: [PATCH 3/3] Update longest_increasing_subsequence_iterative.py --- dynamic_programming/longest_increasing_subsequence_iterative.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/longest_increasing_subsequence_iterative.py b/dynamic_programming/longest_increasing_subsequence_iterative.py index 9a25d517cce5..665c86a35d2e 100644 --- a/dynamic_programming/longest_increasing_subsequence_iterative.py +++ b/dynamic_programming/longest_increasing_subsequence_iterative.py @@ -43,7 +43,7 @@ def longest_subsequence(array: list[int]) -> list[int]: for i in range(1, n): for prev in range(i): - # If array[prev] is less than array[i], then + # If array[prev] is less than or equal to array[i], then # longest_increasing_subsequence[prev] + array[i] # is a valid increasing subsequence