该方程的时间复杂度t(n)=t(n-1)*t(n--1)



这个方程的时间复杂度是多少
答案是2^n,但我找不到解决方案。

 t(n)=t(n-1)*t(n-1)

假设基本情况是某个有限常数,它应该在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

相关内容

  • 没有找到相关文章

最新更新