DEV Community

loading...

Discussion on: Daily Challenge #228 - Best Profit in Single Sale

Collapse
craigmc08 profile image
Craig McIlwrath

Simple Haskell solution. Uses the property that the maximum difference of two items in a list will be the difference between the maximum and minimum elements.

maxProfit :: (Ord a, Num a) => [a] -> a
maxProfit [] = 0
maxProfit xs = maximum xs - minimum xs
Collapse
andreasjakof profile image
Andreas Jakof

If max comes before min, then this solution would show a wrong result.

Collapse
craigmc08 profile image
Craig McIlwrath

Oh, I misunderstood the challenge. Didn't notice that order requirement.

Thread Thread
andreasjakof profile image
Andreas Jakof

Donβ€˜t worry... just wanted to point out to you, that you are not done yet. 😬
Keep at it