【发布时间】:2020-02-16 04:25:29
【问题描述】:
我通过实现这篇文章中的算法发现了这个问题:Example of O(n!)?
虽然我尝试计算操作数,但算法在计算 4 时循环了 64 次! (即 24)。
我有什么遗漏吗?或者这个算法真的只是具有不是 O(n!) 的运行时复杂度
var operations = 0;
function factorial(n) {
for (let i = 0; i < n; i++) {
operations++;
factorial(n - 1);
}
}
console.log(factorial(4))
console.log('operations: ', operations) // prints 64 operations
【问题讨论】:
-
O(n!) 不一定意味着 n!本身。这意味着它与 n! 成正比。阶乘增长很快,所以根本不切实际,但您可能想要将 n 与操作与 n 进行对比!看看这是否使这种关系可信。
标签: javascript algorithm time big-o