Skip to content

Instantly share code, notes, and snippets.

@dsvictor94
Last active August 29, 2015 14:05
Show Gist options
  • Save dsvictor94/dadc6f4a6e29c610c484 to your computer and use it in GitHub Desktop.
Save dsvictor94/dadc6f4a6e29c610c484 to your computer and use it in GitHub Desktop.
Coin Change [Haskell]
import Data.List (foldl1')
import Data.Array
change:: [Int] -> Int -> Int
change cs v = change' v
where
memo = array (0, v) [(x, change'' x) | x <- [0..v]]
change' v = memo ! v
change'' 0 = 0
change'' v = foldl1' min [change' $! (v-i) | i <- takeWhile (<=v) cs] +1
main = do
print $ change [1..4] 100000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment