641. Design Circular Deque Python3

Problem

Design your implementation of the circular double-ended queue (deque).

Implement the MyCircularDeque class:

MyCircularDeque(int k) Initializes the deque with a maximum size of k.
boolean insertFront() Adds an item at the front of Deque. Returns true if the operation is successful, or false otherwise.
boolean insertLast() Adds an item at the rear of Deque. Returns true if the operation is successful, or false otherwise.
boolean deleteFront() Deletes an item from the front of Deque. Returns true if the operation is successful, or false otherwise.
boolean deleteLast() Deletes an item from the rear of Deque. Returns true if the operation is successful, or false otherwise.
int getFront() Returns the front item from the Deque. Returns -1 if the deque is empty.
int getRear() Returns the last item from Deque. Returns -1 if the deque is empty.
boolean isEmpty() Returns true if the deque is empty, or false otherwise.
boolean isFull() Returns true if the deque is full, or false otherwise.

Example 1:

Input
["MyCircularDeque", "insertLast", "insertLast", "insertFront", "insertFront", "getRear", "isFull", "deleteLast", "insertFront", "getFront"]
[[3], [1], [2], [3], [4], [], [], [], [4], []]
Output
[null, true, true, true, false, 2, true, true, true, 4]

Explanation

MyCircularDeque myCircularDeque = new MyCircularDeque(3);
myCircularDeque.insertLast(1);  // return True
myCircularDeque.insertLast(2);  // return True
myCircularDeque.insertFront(3); // return True
myCircularDeque.insertFront(4); // return False, the queue is full.
myCircularDeque.getRear();      // return 2
myCircularDeque.isFull();       // return True
myCircularDeque.deleteLast();   // return True
myCircularDeque.insertFront(4); // return True
myCircularDeque.getFront();     // return 4

Constraints:

1 <= k <= 1000
0 <= value <= 1000
At most 2000 calls will be made to insertFront, insertLast, deleteFront, deleteLast, getFront, getRear, isEmpty, isFull.

My code

class MyCircularDeque:

    def __init__(self, k: int):
        self.max = k
        self.data = [None]*self.max
        self.size = 0
        self.rear = 0
        self.front = 0

    def insertFront(self, value: int) -> bool:
        if self.isFull(): return False
        self.front = (self.front - 1 +self.max) % self.max
        self.data[self.front] = value
        self.size += 1
        return True

    def insertLast(self, value: int) -> bool:
        if self.isFull(): return False
        self.data[self.rear] = value
        self.rear = (self.rear + 1) % self.max
        self.size += 1
        return True

    def deleteFront(self) -> bool:
        if self.isEmpty(): return False
        self.data[self.front] = None
        self.front = (self.front + 1) % self.max
        self.size -=1
        return True
        

    def deleteLast(self) -> bool:
        if self.isEmpty(): return False
        self.rear = (self.rear - 1 +self.max) % self.max
        self.data[self.rear]=None
        self.size -=1
        return True

    def getFront(self) -> int:
        if self.isEmpty(): return -1
        return self.data[self.front]

    def getRear(self) -> int:
        if self.isEmpty(): return -1
        return self.data[self.rear-1]

    def isEmpty(self) -> bool:
        return self.size == 0

    def isFull(self) -> bool:
        return self.size == self.max

Review

[실행 결과]
Runtime 69 ms
Memory Usage: 15.2 MB

[접근법]
init : queue와 동일하다
여기에서의 insertLast가 큐에서의 push와 같다고 생각하면 된다. insertFront를 하고 싶으면 해당 자리 앞에 공간을 만들어서 값 넣기
delete는 insert와 동일하지만 비어있는지를 확인해줘야 하고, 사이즈를 하나 줄인다.
나머지는 queue와 동일하다.

[느낀점]
deque와 queue의 차이점
deque를 이중연결로 구현할 때 head와 tail은 실제 값을 가지지 않고 그냥 포인터 !
앞에 넣을때 -1을 해서 공간을 확보해야 함.

좋은 웹페이지 즐겨찾기