[자료구조] python 자료형별 시간복잡도

거북이·2023년 8월 19일
0

Python

목록 보기
18/26
post-thumbnail
  • List functions time complexity
OperationExampleBig-ONotes
IndexI[i]O(1)
lenlen(I)O(1)
appendI.append(5)O(1)
popI.pop()O(1)I.pop(-1)과 동일함
clearI.clear()O(1)I = []과 유사
[:](Slice)I[a:b]O(b-a)I[:] = O(N)
extendI.extend(...)O(len(...))확장 길이에 따라
insertI.insert(i, v)O(N)i위치까지 이동한 후 v를 삽입
deletedel I[i]O(N)
removeI.remove(...)O(N)
containmentin or not inO(N)
min or maxmin(I), max(I)O(N)
reverseI.reverse()O(N)
iterationfor v in I:O(N)
sortI.sort()O(N log N)
  • Dict functions time complexity
OperationExampleBig-ONotes
indexd[k]O(1)
lenlen(d)O(1)
deldel d[k]O(1)
popd.pop()O(1)
popitemd.popitem()O(1)
cleard.clear()O(1)
containmentx in dO(1)
iterationfor k in d:O(N)
  • set functions time complexity
OperationExampleBig-ONotes
x in sO(1)
unionO(len(s)+len(t))
intersectionO(len(s)+len(t))
differenceO(len(s)+len(t))
iterationO(N)
containmentO(1)

주의사항 : List에서는 in 연산의 시간복잡도는 O(N)이지만 Set이나 Dict에서 in 연산의 시간복잡도는 O(1)이다.

https://wiki.python.org/moin/TimeComplexity

0개의 댓글