Skip to content

Instantly share code, notes, and snippets.

@Gaikanomer9
Created September 16, 2020 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gaikanomer9/8f89236dd4524ac09f5749cc87ba700f to your computer and use it in GitHub Desktop.
Save Gaikanomer9/8f89236dd4524ac09f5749cc87ba700f to your computer and use it in GitHub Desktop.
Simple example
# время (O(n1)), память O(n1+2*n2)
# Данное решение использует hash таблицу и позволяет быстро определять - находится ли элементов во втором списке
# Из минусов - выделение дополнительной памяти для сета второго списка
arr1=[1,2,3,4,5]
arr2=[3,4,5,6,7]
def process_list(arr1, arr2):
arr2_s = set(arr2)
return list(filter(lambda x: x not in arr2_s, arr1))
process_list(arr1, arr2)
# Здесь мы используем удобную форму comprehension для прохода через список + изменяем начальный список
# и тем самым не потребляем больше памяти, чем O(1)
array = [0, 1, 0, 0, 4, 5, 6, 0, -4]
array[:] = (el for el in array if el != 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment