Created
January 11, 2016 22:07
-
-
Save wangbj/6a2d8b603590efc27515 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
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