Skip to content

Instantly share code, notes, and snippets.

@reciosonny
Created July 22, 2021 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reciosonny/e4953610aeeb5e484787686c8449bea7 to your computer and use it in GitHub Desktop.
Save reciosonny/e4953610aeeb5e484787686c8449bea7 to your computer and use it in GitHub Desktop.
Unsolved problem from this leetcode problem: https://leetcode.com/problems/design-circular-queue/submissions/
/**
* @param {number} k
*/
var MyCircularQueue = function(k) {
this.arr = new Array(k);
this.headCount = 0;
this.tailCount = 0;
};
/**
* @param {number} value
* @return {boolean}
*/
MyCircularQueue.prototype.enQueue = function(value) {
if (this.tailCount > this.arr.length-1) return false;
this.arr[this.tailCount] = value;
this.tailCount+=1;
return true;
};
/**
* @return {boolean}
*/
MyCircularQueue.prototype.deQueue = function() {
delete this.arr[0]; //first one will always be dequeued
this.arr = putEmptyElementsInLast(this.arr);
this.headCount +=1;
this.tailCount -=1;
return true;
};
function putEmptyElementsInLast (arr, numToRearrangeArray) {
const arrTest = [...arr];
for(i=0; i<arrTest.length; i++) {
var p1 = arrTest[i];
var p2 = arrTest[i+1];
if(isNaN(p1) && !isNaN(p2)) {
arrTest[i] = p2;
delete arrTest[i+1]; //remove the next element value so we can move the contents
}
// console.log('p1: ', p1, '||| p2: ', p2);
if (isNaN(p2) && (arrTest.length) === i) break;
}
return arrTest;
}
/**
* @return {number}
*/
MyCircularQueue.prototype.Front = function() {
return this.arr[0] ?? -1;
};
/**
* @return {number}
*/
MyCircularQueue.prototype.Rear = function() {
return this.arr[this.arr.length-1] ?? -1;
};
/**
* @return {boolean}
*/
MyCircularQueue.prototype.isEmpty = function() {
return this.arr.filter(x => x || !isNaN(x)).length === 0;
};
/**
* @return {boolean}
*/
MyCircularQueue.prototype.isFull = function() {
return this.arr.filter(x => x || !isNaN(x)).length > 0;
};
/**
* Your MyCircularQueue object will be instantiated and called as such:
* var obj = new MyCircularQueue(k)
* var param_1 = obj.enQueue(value)
* var param_2 = obj.deQueue()
* var param_3 = obj.Front()
* var param_4 = obj.Rear()
* var param_5 = obj.isEmpty()
* var param_6 = obj.isFull()
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment