Skip to content

Instantly share code, notes, and snippets.

@olajep
Last active August 23, 2019 11:37
Show Gist options
  • Save olajep/7236551 to your computer and use it in GitHub Desktop.
Save olajep/7236551 to your computer and use it in GitHub Desktop.
rainSolver = solve 0 . zipWithRightMax
zipWithRightMax xs = zip xs maxVals
where
rev = reverse xs
(maxVals, _) = foldl maxFold ([], head rev) rev
maxFold (acc, maxVal) x = (maxVal:acc,maxFromHere)
where
maxFromHere = max maxVal x
solve acc [] = acc
solve acc (x:xs)
| xs' == [] = solve acc xs -- no right border -> this x cannot be left border
| otherwise = solve (acc+area) xs'
where
(val, rightMax) = x
height = min val rightMax
(elems',xs') = span ((<height).fst) xs
elems = fst $ unzip elems'
area = (length elems * height) - (sum elems)
rainSolver = solve 0
solve acc [] = acc
solve acc (x:xs)
| xs' == [] = solve acc xs -- no right border -> this x cannot be left border
| otherwise = solve (acc+area) xs'
where
height = minimum [x, maximum xs]
(elems,xs') = span (<height) xs
area = (length elems * height) - (sum elems)
@Bontrey
Copy link

Bontrey commented Oct 30, 2013

Oh yeah. Good point. Each element is only segmented out once

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment