Created
July 24, 2019 20:20
-
-
Save kell18/a1f00f66632cbfe151cec888a4808059 to your computer and use it in GitHub Desktop.
This file contains 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 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