【发布时间】:2022-09-28 04:14:23
【问题描述】:
我在确定以下代码行的原始操作计数时遇到了一些麻烦
def question1(n):
n = n # 1 ops
i = 0 # 1 ops
a = 0 # 1 ops
while i < n: # n ops
j = 0 # n ops
while j < n: # n * n ops
k = 0 # n * n ops
while k < 60: # n * n * 60 ops
a = i * j - i * 2 + k # n * n * 60 * 5 ops
k += 1 # n * n * 60 * 2 ops
j += 1 # n * n ops
i += 1 # n ops
# total sum of prim operations = (n * n * 483) + (3 * n) + 3
我不确定是否
while k < 60: # n * n * 60 ops
a = i * j - i * 2 + k # n * n * 60 * 5 ops
k += 1 # n * n * 60 * 2 ops
真的吗
n * n * 60?
或者应该是
n * n * n * 60
-
最里面的循环应该被视为
O(1)操作(如果所有二进制操作都被视为O(1)操作)。 -
大 O 的全部意义在于忽略乘法常数(以及非显性项),因此 O(60*n^k) 与 O(n^k) 相同。
-
如果我们忽略常量,这应该是 O(n^3) 还是 O(n^2) ?我听到了不同的答案,我很困惑
-
你的表达式中没有n^3,那为什么应该是n^3呢?
-
我认为 Big Oh 表示法是 O(n^3),因为有三个 while 循环,但人们说最内层循环不计在内,因为它是常数,所以 Big Oh 是 O(n^2)
标签: python algorithm time time-complexity big-o