Skip to content

Instantly share code, notes, and snippets.

@rjeli
Created November 8, 2015 07:34
Show Gist options
  • Select an option

  • Save rjeli/60eff69b5176a1e07646 to your computer and use it in GitHub Desktop.

Select an option

Save rjeli/60eff69b5176a1e07646 to your computer and use it in GitHub Desktop.
Hardcore DP
profit :: [Int] -> Int
profit xs = f (head xs) 0 (tail xs)
where
f minVal maxDiff [] = maxDiff
f minVal maxDiff (x:xs) = f (min minVal x) (max maxDiff (x - minVal)) xs
-- O(n) time, O(1) memory
@rjeli

rjeli commented Nov 8, 2015

Copy link
Copy Markdown
Author

should be profit (x:xs) = f x 0 xs for better style points

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment