Last active
December 29, 2015 02:39
-
-
Save olim7t/7602197 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
// Naive solution (two passes) | |
def volume(l: List[Int]) = { | |
def peakRight(l: List[Int]) = l.foldRight(List(0)) { case (i, acc) => (i max acc.head) :: acc }.tail | |
def peakLeft(l: List[Int]) = peakRight(l.reverse).reverse | |
val waterLevels = peakLeft(l) zip peakRight(l) map { case (l, r) => l min r } | |
val volumes = l zip waterLevels map { case (g, w) => (w - g) max 0 } | |
volumes reduceLeft (_ + _) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment