Skip to content

Latest commit

 

History

History
138 lines (108 loc) · 4.14 KB

2022.07.01.md

File metadata and controls

138 lines (108 loc) · 4.14 KB

Stack & Queue


Design Circular Queue

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.


파이썬 class랑 method, global variable 등의 성질을 몰라서 진짜 뻘짓 엄청 했다..

My code:

    class MyCircularQueue(object):
    arr = []
    head = -1
    tail = -1
    
    def __init__(self, k):
        """
        :type k: int
        """
        
        self.k = k
        for i in range(self.k):
            self.arr.append(-1)
        

    def enQueue(self, value):
        """
        :type value: int
        :rtype: bool
        """
        
        if self.head == -1 and self.tail == -1:
            self.head += 1
            self.tail += 1
            self.arr[self.tail] = value
        elif self.tail - self.head == self.k-1 or self.head - self.tail == 1:
            return False
        elif self.tail == self.k-1:
            self.tail = 0
            self.arr[self.tail] = value
        else:
            self.tail += 1
            self.arr[self.tail] = value
        #print(self.arr)
        return True
            
        
        

    def deQueue(self):
        """
        :rtype: bool
        """
        if self.head == -1 and self.tail == -1:
            return False
        elif self.head == self.tail:
            self.arr[self.head] = -1
            self.head = -1
            self.tail = -1
        elif self.head == self.k-1:
            self.arr[self.head] = -1
            self.head = 0
        else:
            self.arr[self.head] = -1
            self.head += 1
        return True
        

    def Front(self):
        """
        :rtype: int
        """
        if self.head == -1:
            return -1
        else:
            return self.arr[self.head]
        

    def Rear(self):
        """
        :rtype: int
        """
        if self.tail == -1:
            return -1
        else:
            return self.arr[self.tail]
        

    def isEmpty(self):
        """
        :rtype: bool
        """
        return self.head == -1 and self.tail == -1
        

    def isFull(self):
        """
        :rtype: bool
        """
        return self.head - self.tail == 1 or self.tail - self.head == self.k-1
        


# Your MyCircularQueue object will be instantiated and called as such:
# obj = MyCircularQueue(k)
# param_1 = obj.enQueue(value)
# param_2 = obj.deQueue()
# param_3 = obj.Front()
# param_4 = obj.Rear()
# param_5 = obj.isEmpty()
# param_6 = obj.isFull()

우왁!!!!runtime 처음으로 상위 1.5정도에 들었다 ㅠㅠㅠㅠ 솔직히 예상도 못했는데, 진짜 정직하게 잘 짜서 그런가? 하나 아쉬운 것은 enqueue랑 dequeue랑 과정에서 arr의 isEmpty와 isFull 여부를 파악하는 조건문이 있는데, 거기서 isEmpty, isFull 메서드를 사용하고 싶었는데 아직 그 방법을 못알아냈다. 다른 클래스에 만들고 상속..?하면 될 것 같기도 하고.