622. Design Circular Queue Python3

Problem

Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. It is also called "Ring Buffer".

One of the benefits of the circular queue is that we can make use of the spaces in front of the queue. In a normal queue, once the queue becomes full, we cannot insert the next element even if there is a space in front of the queue. But using the circular queue, we can use the space to store new values.

Implementation the MyCircularQueue class:

MyCircularQueue(k) Initializes the object with the size of the queue to be k.
int Front() Gets the front item from the queue. If the queue is empty, return -1.
int Rear() Gets the last item from the queue. If the queue is empty, return -1.
boolean enQueue(int value) Inserts an element into the circular queue. Return true if the operation is successful.
boolean deQueue() Deletes an element from the circular queue. Return true if the operation is successful.
boolean isEmpty() Checks whether the circular queue is empty or not.
boolean isFull() Checks whether the circular queue is full or not.
You must solve the problem without using the built-in queue data structure in your programming language.

Example 1:

Input
["MyCircularQueue", "enQueue", "enQueue", "enQueue", "enQueue", "Rear", "isFull", "deQueue", "enQueue", "Rear"]
[[3], [1], [2], [3], [4], [], [], [], [4], []]
Output
[null, true, true, true, false, 3, true, true, true, 4]

Explanation

MyCircularQueue myCircularQueue = new MyCircularQueue(3);
myCircularQueue.enQueue(1); // return True
myCircularQueue.enQueue(2); // return True
myCircularQueue.enQueue(3); // return True
myCircularQueue.enQueue(4); // return False
myCircularQueue.Rear();     // return 3
myCircularQueue.isFull();   // return True
myCircularQueue.deQueue();  // return True
myCircularQueue.enQueue(4); // return True
myCircularQueue.Rear();     // return 4

Constraints:

1 <= k <= 1000
0 <= value <= 1000
At most 3000 calls will be made to enQueue, deQueue, Front, Rear, isEmpty, and isFull.

My code

class MyCircularQueue:

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

    def enQueue(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 deQueue(self) -> bool:
        if self.isEmpty(): return False
        self.data[self.rear] = 0
        self.front = (self.front + 1) % self.max
        self.size -= 1
        return True
        

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

    def Rear(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: 64 ms Memory Usage: 14.7 MB

[접근법]
init : 큐의 길이를 정해준다. 큐의 앞과 뒤, 사이즈를 가르키는 포인터에 0을 할당해준다.
enQueue : 예외처리 후 해당 위치에 원소를 삽입하고 rear를 다음 위치로 이동한다. 여기서 max길이로 나눈 나머지를 할당해서 최대 크기를 벗어나지 않도록 한다.
deQueue : front포인터를 이동시킨다. 원소가 있으면 제거하고 front를 다음 위치로 이동시킨다.

[느낀점]
맨 처음에 [0]을 주면 안된다. 입력이 0이 주어질 수 있기 때문.
코드 쓸때 순서 주의할 것!
처음에 포인터가 가르키는게 뭔지 인지하기

좋은 웹페이지 즐겨찾기