Last active
October 23, 2021 15:06
-
-
Save sshark/d603246ca4e40a36e3cff19c0815f1f3 to your computer and use it in GitHub Desktop.
Max difference between increasing elemets
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
import Math.* | |
def maxDiff(xs: List[Int]): Int = | |
xs.scanLeft(Int.MaxValue)(min) | |
.tail | |
.zip(xs) | |
.map(x => x._2 - x._1) | |
.filter(_ > 0) | |
.fold(-1)(max) | |
maxDiff(List(7, 1, 5, 4)) // 4 | |
maxDiff(List(8, 5, 3 ,3)) // -1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment