Skip to content

Instantly share code, notes, and snippets.

@wangbj
Created January 11, 2016 22:07
Show Gist options
  • Save wangbj/6a2d8b603590efc27515 to your computer and use it in GitHub Desktop.
Save wangbj/6a2d8b603590efc27515 to your computer and use it in GitHub Desktop.
import qualified Data.ByteString.Char8 as C
import Data.Maybe
import Text.Printf
readPrice = fromJust . readPrice_
where readPrice_ s = C.readInt s >>= \(x, s1) ->
C.uncons s1 >>= \(c, s2) ->
case c of
'.' -> C.readInt s2 >>= \(y, s3) -> return (x*100+y)
_ -> Nothing
getinputs = map readPrice . C.words
maxProfitAfter x [] = Nothing
maxProfitAfter x xs = if y <= x then Nothing else Just (x, y)
where y = maximum xs
maxProfit [] = Nothing
maxProfit (x:[]) = Nothing
maxProfit (x:y:xs) = better (maxProfitAfter x xs) (maxProfit (y:xs))
where better Nothing x = x
better x Nothing = x
better l@(Just (x1, y1)) r@(Just (x2, y2)) =
if (y2 - x2) > (y1 - x1) then r else l
main = C.getContents >>= putStrLn . show . maxProfit . getinputs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment