【发布时间】:2015-07-18 04:35:07
【问题描述】:
我遇到了counting change的DP问题的以下解决方案:
count' :: Int -> [Int] -> Int
count' cents coins = aux coins !! cents
where aux = foldr addCoin (1:repeat 0)
where addCoin c oldlist = newlist
where newlist = (take c oldlist) ++ zipWith (+) newlist (drop c oldlist)
它比我天真的自上而下递归解决方案运行得快得多,我仍在努力理解它。
我知道给定一个硬币列表,aux 计算正整数的每个解。因此,数量的解决方案是在该位置索引列表。
不过,我对addCoin 不太清楚。它以某种方式使用每个硬币的价值从硬币列表中提取元素?我正在努力寻找它的直观含义。
aux 中的折叠也将我的大脑打结了。为什么1:repeat 0是初始值?它代表什么?
【问题讨论】:
标签: haskell dynamic-programming