递归的问题是,护理需要注意不要成倍分支或有指数内存足迹;并且还写了一个尾递归函数通常不太具有表现力。
您可以通过动态编程绕过整个递归开销;其具有使用权利折叠在Haskell非常高性能的实现:
count :: (Num a, Foldable t) => t Int -> Int -> a
count coins total = foldr go (1: repeat 0) coins !! total
where
go coin acc = out where out = zipWith (+) acc $ replicate coin 0 ++ out
然后:
\> count [1, 5, 10, 25, 50] 5000
432699251
或如31st problem of Project Euler(1):
\> count [1, 2, 5, 10, 20, 50, 100, 200] 200
73682
甲更少高效的替代方法是使用不可变的非严格(盒装)阵列:
import Data.Array (listArray, (!))
count :: (Num a, Foldable t) => t Int -> Int -> a
count coins total = foldr go init coins ! total
where
init = listArray (0, total) $ 1: repeat 0
go coin arr = out
where
out = listArray (0, total) $ map inc [0..total]
inc i = arr ! i + if i < coin then 0 else out ! (i - coin)
(1)的问题已经在别处公布在计算器;见Using dynamic programming in Haskell? [Warning: ProjectEuler 31 solution inside]
只是猜测这与你在递归的每一步中使用take和drop有很大关系。这些是“O(a)”功能,也许尝试splitAt会是更好的选择。另外,请记住++也是一个“O(a)”操作,因为连接不是使用指针算法完成的,而是通过遍历整个结构。 –
我以为可能是这样,但后来我尝试了一个简单的'recurseone xs = head xs:zipWith(+)(tail xs)(recurseone xs)'并且它仍然很慢 – zoko
您是否明白为什么您的代码在第一名?通常,您可以从正确性证明(正确性属性的“终止”部分)推断资源使用情况(即复杂性约束)。 – d8d0d65b3f7cf42