Skip to content

Commit f391ab6

Browse files
authored
Corrected longest_increasing_subsequence API (#410)
1 parent 7878ee4 commit f391ab6

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

pydatastructs/linear_data_structures/algorithms.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from pydatastructs.linear_data_structures.arrays import (
2-
OneDimensionalArray, DynamicArray, Array)
2+
OneDimensionalArray, DynamicArray, DynamicOneDimensionalArray, Array)
33
from pydatastructs.utils.misc_util import _check_type, _comp
44
from concurrent.futures import ThreadPoolExecutor
55
from math import log, floor
@@ -1010,16 +1010,18 @@ def longest_increasing_subsequence(array):
10101010
>>> from pydatastructs import longest_increasing_subsequence as LIS
10111011
>>> array = ODA(int, [2, 5, 3, 7, 11, 8, 10, 13, 6])
10121012
>>> longest_inc_subsequence = LIS(array)
1013-
>>> longest_inc_subsequence
1014-
[2, 3, 7, 8, 10, 13]
1013+
>>> str(longest_inc_subsequence)
1014+
'[2, 3, 7, 8, 10, 13]'
10151015
>>> array2 = ODA(int, [3, 4, -1, 5, 8, 2, 2 ,2, 3, 12, 7, 9, 10])
10161016
>>> longest_inc_subsequence = LIS(array2)
1017-
>>> longest_inc_subsequence
1018-
[-1, 2, 3, 7, 9, 10]
1017+
>>> str(longest_inc_subsequence)
1018+
'[-1, 2, 3, 7, 9, 10]'
10191019
"""
10201020
n = len(array)
1021-
dp = [0]*n
1022-
parent = [-1]*n
1021+
dp = OneDimensionalArray(int, n)
1022+
dp.fill(0)
1023+
parent = OneDimensionalArray(int, n)
1024+
parent.fill(-1)
10231025
length = 0
10241026
for i in range(1, n):
10251027
if array[i] <= array[dp[0]]:
@@ -1033,13 +1035,16 @@ def longest_increasing_subsequence(array):
10331035
ceil = lower_bound(curr_array, array[i])
10341036
dp[ceil] = i
10351037
parent[i] = dp[ceil - 1]
1036-
ans = []
1037-
1038+
ans = DynamicOneDimensionalArray(int, 0)
10381039
last_index = dp[length]
10391040
while last_index != -1:
1040-
ans[:0] = [array[last_index]]
1041+
ans.append(array[last_index])
10411042
last_index = parent[last_index]
1042-
return ans
1043+
n = ans._last_pos_filled + 1
1044+
ans_ODA = OneDimensionalArray(int, n)
1045+
for i in range(n):
1046+
ans_ODA[n-1-i] = ans[i]
1047+
return ans_ODA
10431048

10441049
def _permutation_util(array, start, end, comp, perm_comp):
10451050
size = end - start + 1

pydatastructs/linear_data_structures/tests/test_algorithms.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,27 +251,27 @@ def test_longest_increasing_subsequence():
251251
arr1 = ODA(int, [2, 5, 3, 7, 11, 8, 10, 13, 6])
252252
output = longest_increasing_subsequence(arr1)
253253
expected_result = [2, 3, 7, 8, 10, 13]
254-
assert expected_result == output
254+
assert str(expected_result) == str(output)
255255

256256
arr2 = ODA(int, [3, 4, -1, 5, 8, 2, 2, 2, 3, 12, 7, 9, 10])
257257
output = longest_increasing_subsequence(arr2)
258258
expected_result = [-1, 2, 3, 7, 9, 10]
259-
assert expected_result == output
259+
assert str(expected_result) == str(output)
260260

261261
arr3 = ODA(int, [6, 6, 6, 19, 9])
262262
output = longest_increasing_subsequence(arr3)
263263
expected_result = [6, 9]
264-
assert expected_result == output
264+
assert str(expected_result) == str(output)
265265

266266
arr4 = ODA(int, [5, 4, 4, 3, 3, 6, 6, 8])
267267
output = longest_increasing_subsequence(arr4)
268268
expected_result = [3, 6, 8]
269-
assert expected_result == output
269+
assert str(expected_result) == str(output)
270270

271271
arr5 = ODA(int, [7, 6, 6, 6, 5, 4, 3])
272272
output = longest_increasing_subsequence(arr5)
273273
expected_result = [3]
274-
assert expected_result == output
274+
assert str(expected_result) == str(output)
275275

276276
def _test_permutation_common(array, expected_perms, func):
277277
num_perms = len(expected_perms)

0 commit comments

Comments
 (0)