Sponsored
Sponsored
This approach involves using a fixed-size array to represent the deque. We'll maintain two indices, front
and rear
, to manage the current front and last positions in the deque. Operations like insertions and deletions are performed by adjusting these indices while ensuring they wrap around using the modulo operation as necessary to remain within the array bounds.
Time Complexity: O(1) for each operation.
Space Complexity: O(k), where k is the capacity of the deque.
1class MyCircularDeque:
2 def __init__(self, k: int):
3 self.data = [-1] * k
4 self.front = 0
5 self.rear = 0
6 self.size = 0
7 self.capacity = k
8
9 def insertFront(self, value: int) -> bool:
10 if self.isFull():
11 return False
12 self.front = (self.front - 1 + self.capacity) % self.capacity
13 self.data[self.front] = value
14 self.size += 1
15 return True
16
17 def insertLast(self, value: int) -> bool:
18 if self.isFull():
19 return False
20 self.data[self.rear] = value
21 self.rear = (self.rear + 1) % self.capacity
22 self.size += 1
23 return True
24
25 def deleteFront(self) -> bool:
26 if self.isEmpty():
27 return False
28 self.front = (self.front + 1) % self.capacity
29 self.size -= 1
30 return True
31
32 def deleteLast(self) -> bool:
33 if self.isEmpty():
34 return False
35 self.rear = (self.rear - 1 + self.capacity) % self.capacity
36 self.size -= 1
37 return True
38
39 def getFront(self) -> int:
40 if self.isEmpty():
41 return -1
42 return self.data[self.front]
43
44 def getRear(self) -> int:
45 if self.isEmpty():
46 return -1
47 return self.data[(self.rear - 1 + self.capacity) % self.capacity]
48
49 def isEmpty(self) -> bool:
50 return self.size == 0
51
52 def isFull(self) -> bool:
53 return self.size == self.capacity
Python solution uses a list to simulate the circular deque operations. It utilizes the modulo operation to keep track of the indices effectively and allows wrap-around of front
and rear
. The solution checks if the deque is full or empty before insertions or deletions, respectively.
This approach makes use of a doubly linked list to implement the deque. This is particularly effective because it offers dynamic memory usage which can grow or shrink with the number of elements, instead of relying on a pre-allocated fixed-size structure as with arrays.
Time Complexity: O(1) for all operations.
Space Complexity: O(n), where n is the number of elements currently in the deque (potentially more efficient if n is much less than the initial capacity).
1 public int Value;
public Node Next;
public Node Prev;
public Node(int value) {
Value = value;
Next = Prev = null;
}
}
public class MyCircularDeque {
private Node front;
private Node rear;
private int size;
private int capacity;
public MyCircularDeque(int k) {
front = rear = null;
size = 0;
capacity = k;
}
public bool InsertFront(int value) {
if (IsFull()) return false;
Node node = new Node(value);
node.Next = front;
if (front != null) front.Prev = node;
front = node;
if (rear == null) rear = node;
size++;
return true;
}
public bool InsertLast(int value) {
if (IsFull()) return false;
Node node = new Node(value);
node.Prev = rear;
if (rear != null) rear.Next = node;
rear = node;
if (front == null) front = node;
size++;
return true;
}
public bool DeleteFront() {
if (IsEmpty()) return false;
front = front.Next;
if (front != null) front.Prev = null;
else rear = null;
size--;
return true;
}
public bool DeleteLast() {
if (IsEmpty()) return false;
rear = rear.Prev;
if (rear != null) rear.Next = null;
else front = null;
size--;
return true;
}
public int GetFront() {
return IsEmpty() ? -1 : front.Value;
}
public int GetRear() {
return IsEmpty() ? -1 : rear.Value;
}
public bool IsEmpty() {
return size == 0;
}
public bool IsFull() {
return size == capacity;
}
}
In C#, the linked list model for the deque supports dynamic sizing, letting each node connect bidirectionally, which allows rapid pointer modifications for diverse operations at both front and end.