Skip to content

Instantly share code, notes, and snippets.

@Centaur
Created October 15, 2012 10:05
Show Gist options
  • Save Centaur/3891791 to your computer and use it in GitHub Desktop.
Save Centaur/3891791 to your computer and use it in GitHub Desktop.
max sub seq
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