[英]Why does O(n*n*n!) get simplified to O((n+2)!) when calculation big O
So I have have been reading the cracking the coding interview book and there is a problem where we're we have a function that does O(n* n* n.) work.所以我一直在阅读破解编码面试书,有一个问题是我们有一个 function 可以 O(n* n* n.) 工作。 The book then says this can be expressed by O((n+2).).然后书上说这可以用 O((n+2).) 来表示。 It says Similarly O(n*n?)can be expressed by O((n+1).).它说类似地 O(n*n?) 可以表示为 O((n+1).)。 I looked in all rules if permutations and did not find any way to logically get there .如果排列,我查看了所有规则,但没有找到任何合乎逻辑的方法。 My first step was cool I have O(n^2 +n!) now what?我的第一步很酷我有 O(n^2 +n!) 现在怎么办? I don't know what steps to take next .我不知道接下来要采取什么步骤。
You already know (I think) that n. = 1*2*3*...*n
你已经知道(我认为) n. = 1*2*3*...*n
n. = 1*2*3*...*n
. n. = 1*2*3*...*n
。 So n*n*n. = 1*2*3*...*n*n*n
所以n*n*n. = 1*2*3*...*n*n*n
n*n*n. = 1*2*3*...*n*n*n
. n*n*n. = 1*2*3*...*n*n*n
。
As n gets really big, adding 1 or 2 to a factor has a decreasingly significant effect.随着 n 变得非常大,将 1 或 2 加到一个因子上的效果会逐渐降低。 I'm no specialist but what matter with O()
is either the power of n or, in our case, the number in the ()!
我不是专家,但O()
的问题是 n 的幂,或者在我们的例子中是()!
expression.表达。 Which gets us to shorten this to 1*2*3*...*n*(n+1)*(n+2)=(n+2)!
这使我们将其缩短为1*2*3*...*n*(n+1)*(n+2)=(n+2)!
. .
Eventually, O(n*n*n!)
can be expressed O((n+2)!)
.最终, O(n*n*n!)
可以表示为O((n+2)!)
。
To calculatee x!
计算x!
you do x*(x-1)!
你做x*(x-1)!
recursively until x-1==1
so x.==(x-1)*(x-2)*...*1
is O(n.).递归直到x-1==1
所以x.==(x-1)*(x-2)*...*1
是 O(n.)。 Therefore to do x*x!
因此要做x*x!
we have (x-0)*(x-1)*...*1
which takes one extra call to our recursive function (but at the beginning, with large x value) ie (x+1)!
我们有(x-0)*(x-1)*...*1
对我们的递归 function 进行一次额外调用(但在开始时,x 值很大)即(x+1)!
iterations.迭代。 Similarly, (x-0)*(x-0)*(x-1)*(x-2)*...*1==x²*x!
同样, (x-0)*(x-0)*(x-1)*(x-2)*...*1==x²*x!
requires (x+2)!
需要(x+2)!
function evaluations to compute, hence O((n+2).) efficiency. function 评估计算,因此 O((n+2).) 效率。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.