Created
October 15, 2012 10:05
-
-
Save Centaur/3891791 to your computer and use it in GitHub Desktop.
max sub seq
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def maxseq(data: List[Int]): List[Int] = { | |
@annotation.tailrec | |
def maxAccum(remain: List[Int], found: List[Int], next: List[Int]): List[Int] = remain match { | |
case Nil => if(found.size == 1) Nil else found | |
case head :: tail => if(head > next.head) { | |
if(next.length == found.length) maxAccum(tail, head :: next, head :: next) | |
else maxAccum(tail, found, head :: next) | |
} | |
else maxAccum(tail, next, head :: Nil) | |
} | |
maxAccum(data.tail, data.head :: Nil, data.head :: Nil).reverse | |
} | |
maxseq(1 :: 0 :: 1 :: 2 :: 3 :: 0 :: 4 :: 5 :: Nil) | |
maxseq(5 :: 6 :: 1 :: 3 :: 4 :: Nil) | |
maxseq(5 :: 6 :: 1 :: 3 :: Nil) | |
maxseq(8 :: 7 :: 6 :: 5 :: Nil) | |
maxseq(1 :: 2 :: Nil) | |
maxseq(2 :: 1 :: Nil) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment