Skip to content

Commit b5269b4

Browse files
Enhancements to "Longest Increasing Subsequence" (#209)
* perf(LongSubSeq): use pushfirst! instead of push! then reverse! * refactor(LongSubSeq): generalize the lis methods to accept any Integer subtype * test(LongSubSeq): test `lis` on all Integer subtypes --------- Co-authored-by: Soc Virnyl S. Estela <[email protected]>
1 parent b0a4123 commit b5269b4

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

src/longest_increasing_subsequence/binary_search.jl

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
lis(arr::Array{Int}, ::Val{:bs})
2+
lis(arr::Array{<:Integer}, ::Val{:bs})
33
44
# Arguments:
55
- `arr`: sequence of integers
@@ -23,12 +23,12 @@ https://cp-algorithms.com/sequences/longest_increasing_subsequence.html
2323
- [Igor Malheiros](https://github.com/igormalheiros)
2424
"""
2525

26-
function lis(arr::Array{Int}, ::Val{:bs})
26+
function lis(arr::Array{T}, ::Val{:bs}) where T <: Integer
2727
len = length(arr)
28-
memo = ones(Int, len)
28+
memo = ones(T, len)
2929
p = ones(Int, len)
3030

31-
lis_arr = Int[]
31+
lis_arr = T[]
3232

3333
len == 0 && return lis_arr # if `arr` is empty
3434

@@ -48,12 +48,10 @@ function lis(arr::Array{Int}, ::Val{:bs})
4848
last_pos = lis_len
4949
for i in len:-1:1
5050
if p[i] == last_pos
51-
push!(lis_arr, arr[i])
51+
pushfirst!(lis_arr, arr[i])
5252
last_pos -= 1
5353
end
5454
end
5555

56-
reverse!(lis_arr)
57-
5856
return lis_arr
5957
end

src/longest_increasing_subsequence/dynamic_programming.jl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
lis(arr::Array{Int}, ::Val{:dp})
2+
lis(arr::Array{<:Integer}, ::Val{:dp})
33
44
# Arguments:
55
- `arr`: sequence of integers
@@ -23,12 +23,12 @@ https://cp-algorithms.com/sequences/longest_increasing_subsequence.html
2323
- [Igor Malheiros](https://github.com/igormalheiros)
2424
"""
2525

26-
function lis(arr::Array{Int}, ::Val{:dp})
26+
function lis(arr::Array{T}, ::Val{:dp}) where T <: Integer
2727
len = length(arr)
2828
memo = ones(Int, len)
2929
p = zeros(Int, len)
3030

31-
lis_arr = Int[]
31+
lis_arr = T[]
3232

3333
len == 0 && return lis_arr # if arr is empty
3434

@@ -51,11 +51,9 @@ function lis(arr::Array{Int}, ::Val{:dp})
5151

5252
# Restoring
5353
while lis_pos != 0
54-
push!(lis_arr, arr[lis_pos])
54+
pushfirst!(lis_arr, arr[lis_pos])
5555
lis_pos = p[lis_pos]
5656
end
5757

58-
reverse!(lis_arr)
59-
6058
return lis_arr
6159
end

test/longest_increasing_subsequence.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ using TheAlgorithms.LongSubSeq
1919
# two possible results:
2020
@test lis([3, 4, -1, 5, 8, 2, 3, 12, 7, 9, 10], Val(:dp)) in
2121
[[-1, 2, 3, 7, 9, 10], [3, 4, 5, 8, 9, 10]]
22+
# Boolean array
23+
@test lis([true, false, false, true], Val(:dp)) == [false, true]
24+
# other Integer subtypes
25+
for T in [UInt128, UInt16, UInt32, UInt64, UInt8, BigInt, Int128, Int16, Int32, Int64, Int8]
26+
@test lis(T[3, 10, 2, 1, 20], Val(:dp)) == T[3, 10, 20]
27+
end
2228
end
2329

2430
@testset "LIS: Binary Search approach!" begin
@@ -39,5 +45,11 @@ using TheAlgorithms.LongSubSeq
3945
# two possible results:
4046
@test lis([3, 4, -1, 5, 8, 2, 3, 12, 7, 9, 10], Val(:bs)) in
4147
[[-1, 2, 3, 7, 9, 10], [3, 4, 5, 8, 9, 10]]
48+
# Boolean array
49+
@test lis([true, false, false, true], Val(:bs)) == [false, true]
50+
# other Integer subtypes
51+
for T in [UInt128, UInt16, UInt32, UInt64, UInt8, BigInt, Int128, Int16, Int32, Int64, Int8]
52+
@test lis(T[3, 10, 2, 1, 20], Val(:bs)) == T[3, 10, 20]
53+
end
4254
end
4355
end

0 commit comments

Comments
 (0)