Last active
August 15, 2017 20:48
-
-
Save dhanji/e1f959d9f5335ef8ad3119d25984e93e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
-- Largest interval algorithm. | |
-- "Compute the largest profit to be gained from a series of prices" | |
-- | |
profits smallest largest gains [] = gains | |
profits smallest largest gains (p:ps) | |
| p < smallest = profits p p (gains ++ [largest - smallest]) ps | |
| p > largest = profits smallest p (gains ++ [p - smallest]) ps | |
| otherwise = profits smallest largest gains ps | |
bestProfit [] = 0 | |
bestProfit ls = foldr max (0) (profits first first [] ls) | |
where | |
first = ls !! 0 | |
main = putStrLn $ show $ bestProfit prices | |
where | |
prices = [10, 7, 5, 8, 11, 9, 4, 20] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment