Problem solution · Python

Design Circular Deque

Design Circular Deque: a Python solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Direct simulation
Source
Kamyu LeetCode Solutions
Length
91 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For Design Circular Deque, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 91 lines of Python from the credited upstream file design-circular-deque.py.
  • The implementation visibly relies on work queue.
  • No explicit loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeDesign Circular Deque · PythonPython
Use this to learn the idea, then write your own version.
# Time:  O(1)# Space: O(k) class MyCircularDeque(object):     def __init__(self, k):        """        Initialize your data structure here. Set the size of the deque to be k.        :type k: int        """        self.__start = 0        self.__size = 0        self.__buffer = [0] * k     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        self.__start = (self.__start-1) % len(self.__buffer)        self.__buffer[self.__start] = 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        self.__buffer[(self.__start+self.__size) % len(self.__buffer)] = 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.__start = (self.__start+1) % len(self.__buffer)        self.__size -= 1        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.__size -= 1        return True     def getFront(self):        """        Get the front item from the deque.        :rtype: int        """        return -1 if self.isEmpty() else self.__buffer[self.__start]     def getRear(self):        """        Get the last item from the deque.        :rtype: int        """        return -1 if self.isEmpty() else self.__buffer[(self.__start+self.__size-1) % len(self.__buffer)]     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 == len(self.__buffer)    

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗