Skip to content

Latest commit

 

History

History
115 lines (80 loc) · 2.56 KB

_641. Design Circular Deque.md

File metadata and controls

115 lines (80 loc) · 2.56 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : September 30, 2024

Last updated : September 30, 2024


Related Topics : Array, Linked List, Design, Queue

Acceptance Rate : 64.67 %


Solutions

Python

class MyCircularDeque:

    def __init__(self, k: int):
        self.s = 0
        self.b = 0
        self.size = 0
        self.maxSize = k
        self.deque = [0] * k

    def insertFront(self, value: int) -> bool:
        if self.size >= self.maxSize :
            return False

        self.s = (self.s + 1) % self.maxSize
        self.deque[self.s] = value

        if not self.size :
            self.b = self.s

        self.size += 1
        return True

    def insertLast(self, value: int) -> bool:
        if self.size >= self.maxSize :
            return False

        self.b = (self.b - 1 + self.maxSize) % self.maxSize
        self.deque[self.b] = value

        if not self.size :
            self.s = self.b

        self.size += 1
        return True

    def deleteFront(self) -> bool:
        if self.size == 0 :
            return False

        self.size -= 1

        if not self.size :
            self.s = self.b
        else :
            self.s = (self.s - 1 + self.maxSize) % self.maxSize

        return True


    def deleteLast(self) -> bool:
        if self.size == 0 :
            return False

        self.size -= 1

        if not self.size :
            self.s = self.b
        else :
            self.b = (self.b + 1) % self.maxSize

        return True

    def getFront(self) -> int:
        return self.deque[self.s] if self.size else -1

    def getRear(self) -> int:
        print(f'{self.deque, self.s, self.b = }')
        return self.deque[self.b] if self.size else -1

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

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


# Your MyCircularDeque object will be instantiated and called as such:
# obj = MyCircularDeque(k)
# param_1 = obj.insertFront(value)
# param_2 = obj.insertLast(value)
# param_3 = obj.deleteFront()
# param_4 = obj.deleteLast()
# param_5 = obj.getFront()
# param_6 = obj.getRear()
# param_7 = obj.isEmpty()
# param_8 = obj.isFull()