Deque

  • A deque stands for Double-Ended Queue. It is a special type of data structure that allows you to add and remove elements from both ends efficiently. This makes it useful in applications like task scheduling, sliding window problems and real-time data processing.
  •   from collections import deque
      queue = deque(["Eric", "John", "Michael"])
      queue.append("Terry")           # Terry arrives
      queue.append("Graham")          # Graham arrives
      queue.popleft()                 # The first to arrive now leaves
      queue.popleft()                 # The second to arrive now leaves
      queue                           # Remaining queue in order of arrival

시간 복잡도

  • 삽입
    • append(x) -
      • Add x to the right side of the deque.
    • appendleft(x) -
      • Add x to the left side of the deque.
    • insert(i, x) -
      • Insert x into the deque at position i.
  • 삭제
    • pop() -
      • Remove and return an element from the right side of the deque.
    • popleft() -
      • Remove and return an element from the left side of the deque.
    • remove(x) -
      • Remove the first occurrence of x.
  • 접근
    • de[0] -
      • access the first element.
    • de[-1] -
      • access the last element.
    • de[n] -
      • in the middle. For fast random access, use lists instead.

Reference