Skip to content

Instantly share code, notes, and snippets.

@pfcoperez
Created December 31, 2016 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pfcoperez/03614cf06dfa2f00a2fb3fc49edcc80e to your computer and use it in GitHub Desktop.
Save pfcoperez/03614cf06dfa2f00a2fb3fc49edcc80e to your computer and use it in GitHub Desktop.
def maxSumSubseq(s: Seq[Int]): Int = {
(Option.empty[Int] /: s) {
case (Some(acc), v) if(v < acc+v) => Some(acc+v)
case (_, v) => Some(v)
} max
}
def maxSumSubseqIdxs(s: Seq[Int]): ((Int, Int), Int) = {
val sums = (Option.empty[((Int, Int), Int)] /: s.zipWithIndex) {
case (Some(((i, _), acc)), (v, j)) if(v < acc+v) =>
Some(((i,j), acc+v))
case (_, (v, j)) =>
Some((j, j) -> v)
}
sums.maxBy(_._2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment