Skip to content

Instantly share code, notes, and snippets.

@junjihashimoto
Created March 19, 2014 03:24
Show Gist options
  • Save junjihashimoto/9634952 to your computer and use it in GitHub Desktop.
Save junjihashimoto/9634952 to your computer and use it in GitHub Desktop.
longest increacing subsequence
--import Data.List
--import Debug.Trace
--dat=[1,3,5,0,2,4]
--dat=[10,9,8,7,6,5]
--dat=[39,88,67,5,69,87,82,64,58,61]
dat=[2,1,4,3,6,5,8,7,10,9,12,11,14,13,16,15,18,17,20,19,22,21,24,23,26,25,28,27,30,29,32,31,34,33,36,35,38,37,40,39,42,41,44,43,46,45,48,47,50,49]
--dat=[10,20,11,12]
--tr v = trace (show v) v
less x y = x < y
score::[(Int,(Int,Int))]->(Int,Int)
score f =
let m'= maximum $ map (fst.snd) f
m''= sum $ map (snd.snd) $ filter (\v-> m' == (fst.snd) v) f
in (m',m'')
lis :: [Int]->[(Int,Int)]
lis []=[]
lis (x:xs)=
let xs'::[(Int,Int)]
xs'= lis xs
f::[(Int,(Int,Int))]
f = filter (\(a,_)->x < a) $ zip xs xs'
(m',m'') =score f
in case f of
[] -> (1,1) : xs'
_ -> (m'+1,m'') : xs'
main=do
print $ score $ zip dat $ lis dat
return ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment