Skip to content

Instantly share code, notes, and snippets.

@2016rshah
Created November 8, 2015 15:10
Show Gist options
  • Save 2016rshah/19635b591b08b1f9994d to your computer and use it in GitHub Desktop.
Save 2016rshah/19635b591b08b1f9994d to your computer and use it in GitHub Desktop.
--My initial solution, but I realized that maybe repeatedly calling maximum wasn't good
profits :: [Int] -> [Int]
profits (x:y:ys) = (maximum (xs) - x) : profits (xs)
where xs = y:ys
profits _ = []
--My updated solution that is slightly more complex
--But it keeps track of the current maximum until that current maximum is outdated
profits' :: [Int] -> Int -> [Int]
profits' (x:y:ys) cm =
if x == cm
then profits' xs (maximum xs)
else (cm - x) : (profits' xs cm)
where xs = y:ys
profits' _ _ = []
--Just to test things out
prices :: [Int]
prices = [10, 30, 42, 15, 20, 50, 10, 25] -- 40
prices' :: [Int]
prices' = [30,21,34,52,67,32,45,67,25,78,31,54,64,22] -- 57
main = do
print $ (maximum . profits) prices
print $ (maximum . profits) prices'
print $ (maximum (profits' prices (maximum prices)))
print $ (maximum (profits' prices' (maximum prices')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment