Skip to content

Instantly share code, notes, and snippets.

@kell18
Created July 24, 2019 20:20
Show Gist options
  • Save kell18/a1f00f66632cbfe151cec888a4808059 to your computer and use it in GitHub Desktop.
Save kell18/a1f00f66632cbfe151cec888a4808059 to your computer and use it in GitHub Desktop.
def maxRun(a: Array[Int]): Int = if (a.length < 2) a.length else {
val (totalMaxRun, (prevRun, _)) = a.tail.foldLeft(1 -> (1, a.head)) {
case ((maxRun, (currRun, prevNum)), currNum) if Math.abs(currNum - prevNum) == 1 =>
Math.max(maxRun, currRun + 1) -> (currRun + 1, currNum)
// currNum doesn't belongs to currRun
case ((maxRun, (currRun, prevNum)), currNum) => Math.max(maxRun, currRun) -> (1, currNum)
}
Math.max(totalMaxRun, prevRun)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment