|
| 1 | +/* |
| 2 | +
|
| 3 | +
|
| 4 | +-* Design a Circle Queue *- |
| 5 | +
|
| 6 | +
|
| 7 | +
|
| 8 | +Design your implementation of the circular queue. |
| 9 | +The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) |
| 10 | +principle and the last position is connected back to the first position to make a circle. It is also called "Ring Buffer". |
| 11 | +
|
| 12 | +One of the benefits of the circular queue is that we can make use of the spaces in front of the queue. |
| 13 | +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. |
| 14 | +But using the circular queue, we can use the space to store new values. |
| 15 | +
|
| 16 | +Implementation the MyCircularQueue class: |
| 17 | +
|
| 18 | +MyCircularQueue(k) Initializes the object with the size of the queue to be k. |
| 19 | +int Front() Gets the front item from the queue. If the queue is empty, return -1. |
| 20 | +int Rear() Gets the last item from the queue. If the queue is empty, return -1. |
| 21 | +boolean enQueue(int value) Inserts an element into the circular queue. Return true if the operation is successful. |
| 22 | +boolean deQueue() Deletes an element from the circular queue. Return true if the operation is successful. |
| 23 | +boolean isEmpty() Checks whether the circular queue is empty or not. |
| 24 | +boolean isFull() Checks whether the circular queue is full or not. |
| 25 | +You must solve the problem without using the built-in queue data structure in your programming language. |
| 26 | +
|
| 27 | +
|
| 28 | +
|
| 29 | +Example 1: |
| 30 | +
|
| 31 | +Input |
| 32 | +["MyCircularQueue", "enQueue", "enQueue", "enQueue", "enQueue", "Rear", "isFull", "deQueue", "enQueue", "Rear"] |
| 33 | +[[3], [1], [2], [3], [4], [], [], [], [4], []] |
| 34 | +Output |
| 35 | +[null, true, true, true, false, 3, true, true, true, 4] |
| 36 | +
|
| 37 | +Explanation |
| 38 | +MyCircularQueue myCircularQueue = new MyCircularQueue(3); |
| 39 | +myCircularQueue.enQueue(1); // return True |
| 40 | +myCircularQueue.enQueue(2); // return True |
| 41 | +myCircularQueue.enQueue(3); // return True |
| 42 | +myCircularQueue.enQueue(4); // return False |
| 43 | +myCircularQueue.Rear(); // return 3 |
| 44 | +myCircularQueue.isFull(); // return True |
| 45 | +myCircularQueue.deQueue(); // return True |
| 46 | +myCircularQueue.enQueue(4); // return True |
| 47 | +myCircularQueue.Rear(); // return 4 |
| 48 | +
|
| 49 | +
|
| 50 | +Constraints: |
| 51 | +
|
| 52 | +1 <= k <= 1000 |
| 53 | +0 <= value <= 1000 |
| 54 | +At most 3000 calls will be made to enQueue, deQueue, Front, Rear, isEmpty, and isFull. |
| 55 | +
|
| 56 | +
|
| 57 | +
|
| 58 | +
|
| 59 | +*/ |
| 60 | + |
| 61 | +/* |
| 62 | + * |
| 63 | + * Your MyCircularQueue object will be instantiated and called as such: |
| 64 | + * MyCircularQueue obj = MyCircularQueue(k); |
| 65 | + * bool param1 = obj.enQueue(value); |
| 66 | + * bool param2 = obj.deQueue(); |
| 67 | + * int param3 = obj.Front(); |
| 68 | + * int param4 = obj.Rear(); |
| 69 | + * bool param5 = obj.isEmpty(); |
| 70 | + * bool param6 = obj.isFull(); |
| 71 | + * |
| 72 | + */ |
| 73 | + |
| 74 | +class MyCircularQueue { |
| 75 | +// Runtime: 512 ms, faster than 100.00% of Dart online submissions for Design Circular Queue. |
| 76 | +// Memory Usage: 151.6 MB, less than 50.00% of Dart online submissions for Design Circular Queue. |
| 77 | + // MyCircularQueue |
| 78 | + late List<int> a; |
| 79 | + int front = 0; |
| 80 | + int rear = -1; |
| 81 | + int len = 0; |
| 82 | + MyCircularQueue(int k) { |
| 83 | + this.a = [k]; |
| 84 | + a = List.filled(k, 0); |
| 85 | + this.front; |
| 86 | + this.rear; |
| 87 | + this.len; |
| 88 | + } |
| 89 | + |
| 90 | + bool enQueue(int value) { |
| 91 | + if (!isFull()) { |
| 92 | + if (++rear == a.length) rear = 0; |
| 93 | + a[rear] = value; |
| 94 | + len++; |
| 95 | + return true; |
| 96 | + } else |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + bool deQueue() { |
| 101 | + if (!isEmpty()) { |
| 102 | + if (++front == a.length) front = 0; |
| 103 | + len--; |
| 104 | + return true; |
| 105 | + } else |
| 106 | + return false; |
| 107 | + } |
| 108 | + |
| 109 | + int Front() { |
| 110 | + return isEmpty() ? -1 : a[front]; |
| 111 | + } |
| 112 | + |
| 113 | + int Rear() { |
| 114 | + return isEmpty() ? -1 : a[rear]; |
| 115 | + } |
| 116 | + |
| 117 | + bool isEmpty() { |
| 118 | + return len == 0; |
| 119 | + } |
| 120 | + |
| 121 | + bool isFull() { |
| 122 | + return len == a.length; |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +class MyCircularQueue1 { |
| 127 | + // Runtime: 620 ms, faster than 50.00% of Dart online submissions for Design Circular Queue. |
| 128 | + // Memory Usage: 151.7 MB, less than 50.00% of Dart online submissions for Design Circular Queue. |
| 129 | + int front = -1; |
| 130 | + int rear = -1; |
| 131 | + late List<int> arr; |
| 132 | + int cap = 0; |
| 133 | + MyCircularQueue1(int k) { |
| 134 | + arr = List.filled(k, 0); |
| 135 | + front; |
| 136 | + cap = k; |
| 137 | + rear; |
| 138 | + } |
| 139 | + int next(int i) { |
| 140 | + // to get next idx after i in circular queue |
| 141 | + return (i + 1) % cap; |
| 142 | + } |
| 143 | + |
| 144 | + int prev(int i) { |
| 145 | + // to get prev idx before i in circular queue |
| 146 | + return (i + cap - 1) % cap; |
| 147 | + } |
| 148 | + |
| 149 | + bool enQueue(int value) { |
| 150 | + if (isFull()) return false; |
| 151 | + if (front == -1) { |
| 152 | + front = 0; |
| 153 | + rear = 0; |
| 154 | + arr[rear] = value; |
| 155 | + return true; |
| 156 | + } |
| 157 | + rear = next(rear); |
| 158 | + arr[rear] = value; |
| 159 | + return true; |
| 160 | + } |
| 161 | + |
| 162 | + bool deQueue() { |
| 163 | + if (isEmpty()) return false; |
| 164 | + if (front == rear) { |
| 165 | + front = -1; |
| 166 | + rear = -1; |
| 167 | + return true; |
| 168 | + } |
| 169 | + front = next(front); |
| 170 | + return true; |
| 171 | + } |
| 172 | + |
| 173 | + int Front() { |
| 174 | + if (front == -1) return -1; |
| 175 | + return arr[front]; |
| 176 | + } |
| 177 | + |
| 178 | + int Rear() { |
| 179 | + if (rear == -1) return -1; |
| 180 | + return arr[rear]; |
| 181 | + } |
| 182 | + |
| 183 | + bool isEmpty() { |
| 184 | + return front == -1; |
| 185 | + } |
| 186 | + |
| 187 | + bool isFull() { |
| 188 | + return front != -1 && next(rear) == front; |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +class ListNode { |
| 193 | + late int val; |
| 194 | + ListNode? prev, next; |
| 195 | + ListNode(int x) { |
| 196 | + val = x; |
| 197 | + prev = null; |
| 198 | + next = null; |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +class C { |
| 203 | +// Runtime: 644 ms, faster than 50.00% of Dart online submissions for Design Circular Queue. |
| 204 | +// Memory Usage: 153.5 MB, less than 50.00% of Dart online submissions for Design Circular Queue. |
| 205 | + late int queueSize, currSize; |
| 206 | + late ListNode head, tail; |
| 207 | + // MyCircularQueue |
| 208 | + C(int k) { |
| 209 | + queueSize = k; |
| 210 | + currSize = 0; |
| 211 | + head = ListNode(-1); |
| 212 | + tail = ListNode(-1); |
| 213 | + head.next = tail; |
| 214 | + tail.prev = head; |
| 215 | + } |
| 216 | + |
| 217 | + bool enQueue(int value) { |
| 218 | + if (isFull()) { |
| 219 | + return false; |
| 220 | + } |
| 221 | + ListNode newNode = new ListNode(value); |
| 222 | + newNode.next = tail; |
| 223 | + newNode.prev = tail.prev; |
| 224 | + tail.prev!.next = newNode; |
| 225 | + tail.prev = newNode; |
| 226 | + currSize++; |
| 227 | + return true; |
| 228 | + } |
| 229 | + |
| 230 | + bool deQueue() { |
| 231 | + if (isEmpty()) { |
| 232 | + return false; |
| 233 | + } |
| 234 | + ListNode? toBeDeleted = head.next; |
| 235 | + head.next = toBeDeleted!.next; |
| 236 | + toBeDeleted.next!.prev = head; |
| 237 | + toBeDeleted.next = null; |
| 238 | + toBeDeleted.prev = null; |
| 239 | + currSize--; |
| 240 | + return true; |
| 241 | + } |
| 242 | + |
| 243 | + int Front() { |
| 244 | + if (isEmpty()) { |
| 245 | + return -1; |
| 246 | + } |
| 247 | + return head.next!.val; |
| 248 | + } |
| 249 | + |
| 250 | + int Rear() { |
| 251 | + if (isEmpty()) { |
| 252 | + return -1; |
| 253 | + } |
| 254 | + return tail.prev!.val; |
| 255 | + } |
| 256 | + |
| 257 | + bool isEmpty() { |
| 258 | + return currSize == 0; |
| 259 | + } |
| 260 | + |
| 261 | + bool isFull() { |
| 262 | + return currSize == queueSize; |
| 263 | + } |
| 264 | +} |
0 commit comments