计数变化的时间复杂性

时间:2013-09-28 14:56:22

标签: scala complexity-theory time-complexity asymptotic-complexity

我有类似的问题需要链接:coin change algorithm in scala using recursion

代码是递归的,看起来像:

def countChange(money: Int, coins: List[Int]): Int = {
    def count(capacity: Int, changes: List[Int]): Int = {
            if(capacity == 0) 1
            else if(capacity < 0) 0
            else if(changes.isEmpty && capacity >=1 )0
            else count(capacity, changes.tail) + count(capacity - changes.head, changes)
    }
count(money, coins)
}

我的问题是如何分析此算法的时间复杂度?谢谢!

1 个答案:

答案 0 :(得分:0)

这是树递归的形式,随着输入呈指数增长。 SICP has very nice explanation about this