Answer the question
In order to leave comments, you need to log in
How to calculate the complexity of the comparison operation?
#!/usr/bin/env python3
a = [3, 2, 6, 34, 5] # O(n)
for i in range(5): # O(n)
if a[i] > 5: # ?
print(a[i]) # O(1)
if a[i] > 5:
O(1) + O(1) = O(2) -> константы сокращаем до единицы -> O(1)
O(n) + O(n) + O(1) + O(1) = O(2n) + O(2) -> константы сокращаем до единицы -> O(2n + 1)
Answer the question
In order to leave comments, you need to log in
It's not a dictionary, it's a list. They write that in python, access to an element by index has complexity O (1), they don’t know how lists are implemented in it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question