Возникла путаница с вычислением сложности Big OPython

Программы на Python
Гость
Возникла путаница с вычислением сложности Big O

Сообщение Гость »


Недавно я начал изучать структуры данных на Python

Код: Выделить всё

list_ = [6, 4, 3, 2, 1, 7]
expected_sum_result = 9

def two_pair_sum_using_complement(array, expected_sum):
unique_numbers = set()  #set
for num in array:
if num in unique_numbers:
return True
unique_numbers.add(expected_sum - num)
print(unique_numbers)
return False

print(
two_pair_sum_using_complement(array=list_, expected_sum=expected_sum_result))
In, the above code, you can simply say the BIG O is O(n) but what doubt I have is, that I've used the if num in unique_numbers in operator to have O(n) complexity and I referred to this site https://wiki.python.org/moin/TimeComplexity and I've attached the Screenshot.
So, May I consider I have a FOR loop that is O(n) and I have an in operator that has O(n) inside the FOR loop, so the complexity is O(n^2)?
I've tried by using some websites and AI and I can't understand the things easily.


Источник: https://stackoverflow.com/questions/781 ... complexity

Вернуться в «Python»