这个等式的时间复杂度是多少? 答案是2 ^ n,但我找不到解决方案。
t(n)=t(n-1)*t(n-1)
答案 0 :(得分:2)
假设基本情况是一些有限常量,它应该在O(2^n)
中运行,因为对于每个调用,它会产生两个递归调用。
例如,给定t(1) = constant
t(1) = t(0) * t(0) = constant * constant
t(2) = t(1) * t(1)
t(1) = t(0) * t(0) = constant * constant
t(1) = t(0) * t(0) = constant * constant
t(3) = t(2) * t(2)
t(2) = t(1) * t(1)
t(1) = t(0) * t(0) = constant * constant
t(1) = t(0) * t(0) = constant * constant
t(2) = t(1) * t(1)
t(1) = t(0) * t(0) = constant * constant
t(1) = t(0) * t(0) = constant * constant
然而,通过缓存值可以将其减少到O(n)
(假设没有副作用)
t(1) = t(0) * t(0) = constant * constant
t(2) = t(1) * t(1)
t(1) = t(0) * t(0) = constant * constant
t(1) evaluated in constant time cache lookup
t(3) = t(2) * t(2)
t(2) = t(1) * t(1)
t(1) = t(0) * t(0) = constant * constant
t(1) evaluated in constant time cache lookup
t(2) evaluated in constant time cache lookup