Skip to content

Instantly share code, notes, and snippets.

@dhanji
Last active August 15, 2017 20:48
Show Gist options
  • Save dhanji/e1f959d9f5335ef8ad3119d25984e93e to your computer and use it in GitHub Desktop.
Save dhanji/e1f959d9f5335ef8ad3119d25984e93e to your computer and use it in GitHub Desktop.
-- 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