본문 바로가기

Algorithm/파이썬 알고리즘 인터뷰 스터디

[데크, 우선순위 큐] 26 원형 데크 디자인(Design Circular Deque)

<LeetCode 문제>

https://leetcode.com/problems/design-circular-deque/

 

Design Circular Deque - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

<풀이>

배열을 통해 구현한 풀이는 다음과 같습니다. 크기가 k인 리스트를 배열로 사용합니다.

class MyCircularDeque:

    def __init__(self, k):
        """
        Initialize your data structure here. Set the size of the deque to be k.
        :type k: int
        """
        self._size = 0 #현재 들어있는 원소수
        self._front, self._rear = 0, 0 #deque 앞,뒤의 index
        self._capacity = k #들어갈 수 있는 최대 원소수
        self._data = [-1] * k #-1로 모두 초기화(-1이면 비어있는 상태)

    def insertFront(self, value):
        """
        Adds an item at the front of Deque. Return true if the operation is successful.
        :type value: int
        :rtype: bool
        """
        if self.isFull():
            return False
        if self.isEmpty():
            self._data[self._front] = value
        else:
            self._front = (self._front - 1) % self._capacity #front가 0이면 앞 공간 없으므로
            self._data[self._front] = value
        self._size += 1
        return True

    def insertLast(self, value):
        """
        Adds an item at the rear of Deque. Return true if the operation is successful.
        :type value: int
        :rtype: bool
        """
        if self.isFull():
            return False
        if self.isEmpty():
            self._data[self._rear] = value
        else:
            self._rear = (self._rear + 1) % self._capacity #rear가 맨뒤면 뒷 공간 없으므로
            self._data[self._rear] = value
        self._size += 1
        return True

    def deleteFront(self):
        """
        Deletes an item from the front of Deque. Return true if the operation is successful.
        :rtype: bool
        """
        if self.isEmpty():
            return False
        self._data[self._front] = -1
        self._front = (self._front + 1) % self._capacity #front가 맨뒤면 뒷 공간 없으므로
        self._size -= 1
        if self.isEmpty():
            self._rear = self._front
        return True

    def deleteLast(self):
        """
        Deletes an item from the rear of Deque. Return true if the operation is successful.
        :rtype: bool
        """
        if self.isEmpty():
            return False
        self._data[self._rear] = -1
        self._rear = (self._rear - 1) % self._capacity #rear가 0이면 앞 공간 없으므로
        self._size -= 1
        if self.isEmpty():
            self._front = self._rear
        return True

    def getFront(self):
        """
        Get the front item from the deque.
        :rtype: int
        """
        return self._data[self._front]

    def getRear(self):
        """
        Get the last item from the deque.
        :rtype: int
        """
        return self._data[self._rear]

    def isEmpty(self):
        """
        Checks whether the circular deque is empty or not.
        :rtype: bool
        """
        return self._size == 0

    def isFull(self):
        """
        Checks whether the circular deque is full or not.
        :rtype: bool
        """
        return self._size == self._capacity

 

<학습내용>

1. 파이썬에서 내부에서만 사용한다는 의미로 PEP8에 따라 이름 앞에 _를 붙임
2. 파이썬에서 -1%5=4가 나온다

<학습이 필요한 내용>

1. 힙 관련 시간복잡도(삽입, 삭제, heapify...) (링크)