Skip to content

Instantly share code, notes, and snippets.

@jmcardon
Last active April 8, 2017 18:29
Show Gist options
  • Save jmcardon/68534b9d1ccdd006e551a5a301d80964 to your computer and use it in GitHub Desktop.
Save jmcardon/68534b9d1ccdd006e551a5a301d80964 to your computer and use it in GitHub Desktop.
sealed trait PitStuff
case class Rise(start: Int, end: Int) extends PitStuff
case class Fall(start: Int, end: Int) extends PitStuff
object kek extends App {
def checkMax(current: Int, rise: Option[Rise], fall: Option[Fall], list: List[Int], maxPit: Int): Int ={
list match {
case Nil =>
if(rise.isDefined && fall.isDefined)
Math.max(maxPit, Math.min(fall.get.start - fall.get.end, rise.get.end - rise.get.start))
else
maxPit
case z::zs =>
if(current > z){
rise match {
case None =>
if(fall.isDefined)
checkMax(z, rise, fall.map(_.copy(end=z)), zs, maxPit)
else
checkMax(z, rise, Some(Fall(current, z)), zs, maxPit)
case Some(r) =>
if(fall.isDefined)
checkMax(z, None, Some(Fall(current, z)), zs, Math.max(maxPit, Math.min(fall.get.start - fall.get.end, r.end - r.start)))
else
checkMax(z, None, Some(Fall(current, z)), zs, Math.max(maxPit, Math.min(fall.get.start - fall.get.end, rise.get.end - rise.get.start)))
}
} else if (current < z){
fall match {
case None =>
checkMax(z, None, None, zs, maxPit)
case Some(f) =>
if(rise.isDefined)
checkMax(z,rise.map(_.copy(end=z)),fall,zs,maxPit)
else
checkMax(z,Some(Rise(current,z)),fall,zs,maxPit)
}
}
else {
if(rise.isDefined && fall.isDefined)
checkMax(z, None, None, zs, Math.max(maxPit, Math.min(fall.get.start - fall.get.end, rise.get.end - rise.get.start)))
else
checkMax(z, None, None, zs, maxPit)
}
}
}
def deepestPit(a: List[Int]):Int = {
checkMax(a.head,None, None, a.tail, -1)
}
val a: List[Int] = List(0,1,3,-2,0,1,0,-4,2,7)
println(deepestPit(a))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment