Last active
December 10, 2020 22:02
-
-
Save amuradyan/b8c9d0eb494f09225bef863ac57d2699 to your computer and use it in GitHub Desktop.
Hackerrank `Breaking the Records` problem
This file contains hidden or 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 java.io._ | |
import java.math._ | |
import java.security._ | |
import java.text._ | |
import java.util._ | |
import java.util.concurrent._ | |
import java.util.function._ | |
import java.util.regex._ | |
import java.util.stream._ | |
object Solution { | |
case class ProcessedGame(min: Option[Int] = None, max: Option[Int] = None, minBreak: Int = 0, maxBreak: Int = 0) { | |
def getStats = Array(maxBreak, minBreak) | |
} | |
// Complete the breakingRecords function below. | |
def breakingRecords(scores: Array[Int]): Array[Int] = | |
scores.foldLeft(ProcessedGame()) {(acc, e) => | |
acc match { | |
case ProcessedGame(Some(min), Some(max), minBreak, maxBreak) => { | |
if (e > max) ProcessedGame(Some(min), Some(e), minBreak, maxBreak + 1) | |
else if (e < min) ProcessedGame(Some(e), Some(max), minBreak + 1, maxBreak) | |
else acc | |
} | |
case _ => ProcessedGame(Some(e), Some(e)) | |
} | |
}.getStats | |
def main(args: Array[String]): Unit = { | |
val stdin = scala.io.StdIn | |
val n = stdin.readLine.trim.toInt | |
val scores = stdin.readLine.split(" ").map(_.trim.toInt) | |
val result = breakingRecords(scores) | |
println(result.mkString(" ")) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment