|
| 1 | +# 641. Design Circular Deque |
| 2 | + |
| 3 | +- Difficulty: Medium. |
| 4 | +- Related Topics: Array, Linked List, Design, Queue. |
| 5 | +- Similar Questions: Design Circular Queue, Design Front Middle Back Queue. |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +Design your implementation of the circular double-ended queue (deque). |
| 10 | + |
| 11 | +Implement the `MyCircularDeque` class: |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +- `MyCircularDeque(int k)` Initializes the deque with a maximum size of `k`. |
| 16 | + |
| 17 | +- `boolean insertFront()` Adds an item at the front of Deque. Returns `true` if the operation is successful, or `false` otherwise. |
| 18 | + |
| 19 | +- `boolean insertLast()` Adds an item at the rear of Deque. Returns `true` if the operation is successful, or `false` otherwise. |
| 20 | + |
| 21 | +- `boolean deleteFront()` Deletes an item from the front of Deque. Returns `true` if the operation is successful, or `false` otherwise. |
| 22 | + |
| 23 | +- `boolean deleteLast()` Deletes an item from the rear of Deque. Returns `true` if the operation is successful, or `false` otherwise. |
| 24 | + |
| 25 | +- `int getFront()` Returns the front item from the Deque. Returns `-1` if the deque is empty. |
| 26 | + |
| 27 | +- `int getRear()` Returns the last item from Deque. Returns `-1` if the deque is empty. |
| 28 | + |
| 29 | +- `boolean isEmpty()` Returns `true` if the deque is empty, or `false` otherwise. |
| 30 | + |
| 31 | +- `boolean isFull()` Returns `true` if the deque is full, or `false` otherwise. |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +Example 1: |
| 36 | + |
| 37 | +``` |
| 38 | +Input |
| 39 | +["MyCircularDeque", "insertLast", "insertLast", "insertFront", "insertFront", "getRear", "isFull", "deleteLast", "insertFront", "getFront"] |
| 40 | +[[3], [1], [2], [3], [4], [], [], [], [4], []] |
| 41 | +Output |
| 42 | +[null, true, true, true, false, 2, true, true, true, 4] |
| 43 | +
|
| 44 | +Explanation |
| 45 | +MyCircularDeque myCircularDeque = new MyCircularDeque(3); |
| 46 | +myCircularDeque.insertLast(1); // return True |
| 47 | +myCircularDeque.insertLast(2); // return True |
| 48 | +myCircularDeque.insertFront(3); // return True |
| 49 | +myCircularDeque.insertFront(4); // return False, the queue is full. |
| 50 | +myCircularDeque.getRear(); // return 2 |
| 51 | +myCircularDeque.isFull(); // return True |
| 52 | +myCircularDeque.deleteLast(); // return True |
| 53 | +myCircularDeque.insertFront(4); // return True |
| 54 | +myCircularDeque.getFront(); // return 4 |
| 55 | +``` |
| 56 | + |
| 57 | + |
| 58 | +**Constraints:** |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +- `1 <= k <= 1000` |
| 63 | + |
| 64 | +- `0 <= value <= 1000` |
| 65 | + |
| 66 | +- At most `2000` calls will be made to `insertFront`, `insertLast`, `deleteFront`, `deleteLast`, `getFront`, `getRear`, `isEmpty`, `isFull`. |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +## Solution 1 |
| 71 | + |
| 72 | +```javascript |
| 73 | +/** |
| 74 | + * @param {number} k |
| 75 | + */ |
| 76 | +var MyCircularDeque = function(k) { |
| 77 | + this.limit = k; |
| 78 | + this.count = 0; |
| 79 | + this.front = null; |
| 80 | + this.last = null; |
| 81 | +}; |
| 82 | + |
| 83 | +/** |
| 84 | + * @param {number} value |
| 85 | + * @return {boolean} |
| 86 | + */ |
| 87 | +MyCircularDeque.prototype.insertFront = function(value) { |
| 88 | + if (this.count === this.limit) return false; |
| 89 | + var node = { |
| 90 | + value, |
| 91 | + prev: null, |
| 92 | + next: null, |
| 93 | + }; |
| 94 | + if (!this.front) { |
| 95 | + this.front = node; |
| 96 | + this.last = node; |
| 97 | + } else { |
| 98 | + node.next = this.front; |
| 99 | + this.front.prev = node; |
| 100 | + this.front = node; |
| 101 | + } |
| 102 | + this.count += 1; |
| 103 | + return true; |
| 104 | +}; |
| 105 | + |
| 106 | +/** |
| 107 | + * @param {number} value |
| 108 | + * @return {boolean} |
| 109 | + */ |
| 110 | +MyCircularDeque.prototype.insertLast = function(value) { |
| 111 | + if (this.count === this.limit) return false; |
| 112 | + var node = { |
| 113 | + value, |
| 114 | + prev: null, |
| 115 | + next: null, |
| 116 | + }; |
| 117 | + if (!this.last) { |
| 118 | + this.front = node; |
| 119 | + this.last = node; |
| 120 | + } else { |
| 121 | + node.prev = this.last; |
| 122 | + this.last.next = node; |
| 123 | + this.last = node; |
| 124 | + } |
| 125 | + this.count += 1; |
| 126 | + return true; |
| 127 | +}; |
| 128 | + |
| 129 | +/** |
| 130 | + * @return {boolean} |
| 131 | + */ |
| 132 | +MyCircularDeque.prototype.deleteFront = function() { |
| 133 | + if (!this.front) return false; |
| 134 | + if (this.front === this.last) { |
| 135 | + this.front = null; |
| 136 | + this.last = null; |
| 137 | + } else { |
| 138 | + this.front.next.prev = null; |
| 139 | + this.front = this.front.next; |
| 140 | + } |
| 141 | + this.count -= 1; |
| 142 | + return true; |
| 143 | +}; |
| 144 | + |
| 145 | +/** |
| 146 | + * @return {boolean} |
| 147 | + */ |
| 148 | +MyCircularDeque.prototype.deleteLast = function() { |
| 149 | + if (!this.last) return false; |
| 150 | + if (this.front === this.last) { |
| 151 | + this.front = null; |
| 152 | + this.last = null; |
| 153 | + } else { |
| 154 | + this.last.prev.next = null; |
| 155 | + this.last = this.last.prev; |
| 156 | + } |
| 157 | + this.count -= 1; |
| 158 | + return true; |
| 159 | +}; |
| 160 | + |
| 161 | +/** |
| 162 | + * @return {number} |
| 163 | + */ |
| 164 | +MyCircularDeque.prototype.getFront = function() { |
| 165 | + return this.front ? this.front.value : -1; |
| 166 | +}; |
| 167 | + |
| 168 | +/** |
| 169 | + * @return {number} |
| 170 | + */ |
| 171 | +MyCircularDeque.prototype.getRear = function() { |
| 172 | + return this.last ? this.last.value : -1; |
| 173 | +}; |
| 174 | + |
| 175 | +/** |
| 176 | + * @return {boolean} |
| 177 | + */ |
| 178 | +MyCircularDeque.prototype.isEmpty = function() { |
| 179 | + return this.count === 0; |
| 180 | +}; |
| 181 | + |
| 182 | +/** |
| 183 | + * @return {boolean} |
| 184 | + */ |
| 185 | +MyCircularDeque.prototype.isFull = function() { |
| 186 | + return this.count === this.limit; |
| 187 | +}; |
| 188 | + |
| 189 | +/** |
| 190 | + * Your MyCircularDeque object will be instantiated and called as such: |
| 191 | + * var obj = new MyCircularDeque(k) |
| 192 | + * var param_1 = obj.insertFront(value) |
| 193 | + * var param_2 = obj.insertLast(value) |
| 194 | + * var param_3 = obj.deleteFront() |
| 195 | + * var param_4 = obj.deleteLast() |
| 196 | + * var param_5 = obj.getFront() |
| 197 | + * var param_6 = obj.getRear() |
| 198 | + * var param_7 = obj.isEmpty() |
| 199 | + * var param_8 = obj.isFull() |
| 200 | + */ |
| 201 | +``` |
| 202 | + |
| 203 | +**Explain:** |
| 204 | + |
| 205 | +nope. |
| 206 | + |
| 207 | +**Complexity:** |
| 208 | + |
| 209 | +* Time complexity : O(1). |
| 210 | +* Space complexity : O(n). |
| 211 | + |
| 212 | +## Solution 2 |
| 213 | + |
| 214 | +```javascript |
| 215 | +/** |
| 216 | + * @param {number} k |
| 217 | + */ |
| 218 | +var MyCircularDeque = function(k) { |
| 219 | + this.limit = k; |
| 220 | + this.count = 0; |
| 221 | + this.arr = Array(k); |
| 222 | + this.front = -1; |
| 223 | + this.last = -1; |
| 224 | +}; |
| 225 | + |
| 226 | +/** |
| 227 | + * @param {number} value |
| 228 | + * @return {boolean} |
| 229 | + */ |
| 230 | +MyCircularDeque.prototype.insertFront = function(value) { |
| 231 | + if (this.count === this.limit) return false; |
| 232 | + if (this.count === 0) { |
| 233 | + this.front = 0; |
| 234 | + this.last = 0; |
| 235 | + } else { |
| 236 | + this.front -= 1; |
| 237 | + if (this.front < 0) { |
| 238 | + this.front += this.arr.length; |
| 239 | + } |
| 240 | + } |
| 241 | + this.arr[this.front] = value; |
| 242 | + this.count += 1; |
| 243 | + return true; |
| 244 | +}; |
| 245 | + |
| 246 | +/** |
| 247 | + * @param {number} value |
| 248 | + * @return {boolean} |
| 249 | + */ |
| 250 | +MyCircularDeque.prototype.insertLast = function(value) { |
| 251 | + if (this.count === this.limit) return false; |
| 252 | + if (this.count === 0) { |
| 253 | + this.front = 0; |
| 254 | + this.last = 0; |
| 255 | + } else { |
| 256 | + this.last += 1; |
| 257 | + if (this.last >= this.arr.length) { |
| 258 | + this.last -= this.arr.length; |
| 259 | + } |
| 260 | + } |
| 261 | + this.arr[this.last] = value; |
| 262 | + this.count += 1; |
| 263 | + return true; |
| 264 | +}; |
| 265 | + |
| 266 | +/** |
| 267 | + * @return {boolean} |
| 268 | + */ |
| 269 | +MyCircularDeque.prototype.deleteFront = function() { |
| 270 | + if (this.count === 0) return false; |
| 271 | + if (this.count === 1) { |
| 272 | + this.front = -1; |
| 273 | + this.last = -1; |
| 274 | + } else { |
| 275 | + this.front += 1; |
| 276 | + if (this.front >= this.arr.length) { |
| 277 | + this.front -= this.arr.length; |
| 278 | + } |
| 279 | + } |
| 280 | + this.count -= 1; |
| 281 | + return true; |
| 282 | +}; |
| 283 | + |
| 284 | +/** |
| 285 | + * @return {boolean} |
| 286 | + */ |
| 287 | +MyCircularDeque.prototype.deleteLast = function() { |
| 288 | + if (this.count === 0) return false; |
| 289 | + if (this.count === 1) { |
| 290 | + this.front = -1; |
| 291 | + this.last = -1; |
| 292 | + } else { |
| 293 | + this.last -= 1; |
| 294 | + if (this.last < 0) { |
| 295 | + this.last += this.arr.length; |
| 296 | + } |
| 297 | + } |
| 298 | + this.count -= 1; |
| 299 | + return true; |
| 300 | +}; |
| 301 | + |
| 302 | +/** |
| 303 | + * @return {number} |
| 304 | + */ |
| 305 | +MyCircularDeque.prototype.getFront = function() { |
| 306 | + return this.front === -1 ? -1 : this.arr[this.front]; |
| 307 | +}; |
| 308 | + |
| 309 | +/** |
| 310 | + * @return {number} |
| 311 | + */ |
| 312 | +MyCircularDeque.prototype.getRear = function() { |
| 313 | + return this.last === -1 ? -1 : this.arr[this.last]; |
| 314 | +}; |
| 315 | + |
| 316 | +/** |
| 317 | + * @return {boolean} |
| 318 | + */ |
| 319 | +MyCircularDeque.prototype.isEmpty = function() { |
| 320 | + return this.count === 0; |
| 321 | +}; |
| 322 | + |
| 323 | +/** |
| 324 | + * @return {boolean} |
| 325 | + */ |
| 326 | +MyCircularDeque.prototype.isFull = function() { |
| 327 | + return this.count === this.limit; |
| 328 | +}; |
| 329 | + |
| 330 | +/** |
| 331 | + * Your MyCircularDeque object will be instantiated and called as such: |
| 332 | + * var obj = new MyCircularDeque(k) |
| 333 | + * var param_1 = obj.insertFront(value) |
| 334 | + * var param_2 = obj.insertLast(value) |
| 335 | + * var param_3 = obj.deleteFront() |
| 336 | + * var param_4 = obj.deleteLast() |
| 337 | + * var param_5 = obj.getFront() |
| 338 | + * var param_6 = obj.getRear() |
| 339 | + * var param_7 = obj.isEmpty() |
| 340 | + * var param_8 = obj.isFull() |
| 341 | + */ |
| 342 | +``` |
| 343 | + |
| 344 | +**Explain:** |
| 345 | + |
| 346 | +nope. |
| 347 | + |
| 348 | +**Complexity:** |
| 349 | + |
| 350 | +* Time complexity : O(1). |
| 351 | +* Space complexity : O(n). |
0 commit comments