2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -0,0 +1,19 @@
|
|||
defmodule Longest_increasing_subsequence do
|
||||
# Naive implementation
|
||||
def lis(l) do
|
||||
(for ss <- combos(l), ss == Enum.sort(ss), do: ss)
|
||||
|> Enum.max_by(fn ss -> length(ss) end)
|
||||
end
|
||||
|
||||
defp combos(l) do
|
||||
Enum.reduce(1..length(l), [[]], fn k, acc -> acc ++ (combos(k, l)) end)
|
||||
end
|
||||
defp combos(1, l), do: (for x <- l, do: [x])
|
||||
defp combos(k, l) when k == length(l), do: [l]
|
||||
defp combos(k, [h|t]) do
|
||||
(for subcombos <- combos(k-1, t), do: [h | subcombos]) ++ combos(k, t)
|
||||
end
|
||||
end
|
||||
|
||||
IO.inspect Longest_increasing_subsequence.lis([3,2,6,4,5,1])
|
||||
IO.inspect Longest_increasing_subsequence.lis([0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15])
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
defmodule Longest_increasing_subsequence do
|
||||
# Patience sort implementation
|
||||
def patience_lis(l), do: patience_lis(l, [])
|
||||
|
||||
defp patience_lis([h | t], []), do: patience_lis(t, [[{h,[]}]])
|
||||
defp patience_lis([h | t], stacks), do: patience_lis(t, place_in_stack(h, stacks, []))
|
||||
defp patience_lis([], []), do: []
|
||||
defp patience_lis([], stacks), do: get_previous(stacks) |> recover_lis |> Enum.reverse
|
||||
|
||||
defp place_in_stack(e, [stack = [{h,_} | _] | tstacks], prevstacks) when h > e do
|
||||
prevstacks ++ [[{e, get_previous(prevstacks)} | stack] | tstacks]
|
||||
end
|
||||
defp place_in_stack(e, [stack | tstacks], prevstacks) do
|
||||
place_in_stack(e, tstacks, prevstacks ++ [stack])
|
||||
end
|
||||
defp place_in_stack(e, [], prevstacks) do
|
||||
prevstacks ++ [[{e, get_previous(prevstacks)}]]
|
||||
end
|
||||
|
||||
defp get_previous(stack = [_|_]), do: hd(List.last(stack))
|
||||
defp get_previous([]), do: []
|
||||
|
||||
defp recover_lis({e, prev}), do: [e | recover_lis(prev)]
|
||||
defp recover_lis([]), do: []
|
||||
end
|
||||
|
||||
IO.inspect Longest_increasing_subsequence.patience_lis([3,2,6,4,5,1])
|
||||
IO.inspect Longest_increasing_subsequence.patience_lis([0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15])
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
function Get-LongestSubsequence ( [int[]]$A )
|
||||
{
|
||||
If ( $A.Count -lt 2 ) { return $A }
|
||||
|
||||
# Start with an "empty" pile
|
||||
# (We will only store the top value in each "pile".)
|
||||
$Pile = @( [int]::MaxValue )
|
||||
$Last = 0
|
||||
|
||||
# Hashtable to hold the back pointers
|
||||
$BP = @{}
|
||||
|
||||
# For each number in the orginal sequence...
|
||||
ForEach ( $N in $A )
|
||||
{
|
||||
# Find the first pile with a value greater than N
|
||||
$i = 0..$Last | Where { $N -lt $Pile[$_] } | Select -First 1
|
||||
|
||||
# Place N on the pile
|
||||
$Pile[$i] = $N
|
||||
|
||||
# Set the back pointer for this value to the value of the previous pile
|
||||
$BP["$N"] = $Pile[$i-1]
|
||||
|
||||
# If this is the previously empty pile, add a new empty pile
|
||||
If ( $i -eq $Last )
|
||||
{
|
||||
$Pile += @( [int]::MaxValue )
|
||||
$Last++
|
||||
}
|
||||
}
|
||||
|
||||
# Ignore the empty pile
|
||||
$Last--
|
||||
|
||||
# Start with the value of the last pile
|
||||
$N = $Pile[$Last]
|
||||
$S = @( $N )
|
||||
|
||||
# Add the remainder of the values by walking through the back pointers
|
||||
ForEach ( $i in $Last..1 )
|
||||
{
|
||||
$S += ( $N = $BP["$N"] )
|
||||
}
|
||||
|
||||
# Return the series (reversed into the correct order)
|
||||
return $S[$Last..0]
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
( Get-LongestSubsequence 3, 2, 6, 4, 5, 1 ) -join ', '
|
||||
( Get-LongestSubsequence 0, 8, 4, 12, 2, 10, 6, 16, 14, 1, 9, 5, 13, 3, 11, 7, 15 ) -join ', '
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
def longest_increasing_subsequence(X):
|
||||
"""Returns the Longest Increasing Subsequence in the Given List/Array"""
|
||||
N = length(X)
|
||||
P = [0 for i in range(N)]
|
||||
M = [0 for i in range(N+1)]
|
||||
N = len(X)
|
||||
P = [0] * N
|
||||
M = [0] * (N+1)
|
||||
L = 0
|
||||
for i in range(N):
|
||||
lo = 1
|
||||
|
|
@ -24,8 +24,8 @@ def longest_increasing_subsequence(X):
|
|||
S = []
|
||||
k = M[L]
|
||||
for i in range(L-1, -1, -1):
|
||||
S.append(X[k])
|
||||
k = P[k]
|
||||
S.append(X[k])
|
||||
k = P[k]
|
||||
return S[::-1]
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
def powerset[A](s: List[A]) = (0 to s.size).map(s.combinations(_)).reduce(_++_)
|
||||
def isSorted(l:List[Int])(f: (Int, Int) => Boolean) = l.view.zip(l.tail).forall(x => f(x._1,x._2))
|
||||
def sequence(set: List[Int])(f: (Int, Int) => Boolean) = powerset(set).filter(_.nonEmpty).filter(x => isSorted(x)(f)).toList.maxBy(_.length)
|
||||
|
||||
sequence(set)(_<_)
|
||||
sequence(set)(_>_)
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
Function LIS(arr)
|
||||
n = UBound(arr)
|
||||
Dim p()
|
||||
ReDim p(n)
|
||||
Dim m()
|
||||
ReDim m(n)
|
||||
l = 0
|
||||
For i = 0 To n
|
||||
lo = 1
|
||||
hi = l
|
||||
Do While lo <= hi
|
||||
middle = Int((lo+hi)/2)
|
||||
If arr(m(middle)) < arr(i) Then
|
||||
lo = middle + 1
|
||||
Else
|
||||
hi = middle - 1
|
||||
End If
|
||||
Loop
|
||||
newl = lo
|
||||
p(i) = m(newl-1)
|
||||
m(newl) = i
|
||||
If newL > l Then
|
||||
l = newl
|
||||
End If
|
||||
Next
|
||||
Dim s()
|
||||
ReDim s(l)
|
||||
k = m(l)
|
||||
For i = l-1 To 0 Step - 1
|
||||
s(i) = arr(k)
|
||||
k = p(k)
|
||||
Next
|
||||
LIS = Join(s,",")
|
||||
End Function
|
||||
|
||||
WScript.StdOut.WriteLine LIS(Array(3,2,6,4,5,1))
|
||||
WScript.StdOut.WriteLine LIS(Array(0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15))
|
||||
Loading…
Add table
Add a link
Reference in a new issue