[
design
queue
]
BinarySearch 0945 Circular Queue
Problem statement
https://binarysearch.com/problems/Circular-Queue/
Solution
Equal to Leetcode 0622. Design Circular Queue.
Complexity
Time complexity of all operations is O(1)
. Space complexity of total data structure is O(k)
.
Code
class CircularQueue:
def __init__(self, k):
self.CircQueue = [-1]*k
self.k, self.fro, self.end = k, 0, 0
def enqueue(self, value):
if self.isFull(): return False
self.end -= 1
self.CircQueue[self.end % self.k] = value
return True
def dequeue(self):
if self.isEmpty(): return False
self.fro -= 1
self.CircQueue[self.fro % self.k] = -1
return True
def front(self):
if self.isEmpty(): return -1
return self.CircQueue[(self.fro - 1) % self.k]
def top(self):
if self.isEmpty(): return -1
return self.CircQueue[self.end % self.k]
def isEmpty(self):
return self.fro == self.end
def isFull(self):
return self.fro - self.end == self.k