Skip to content

Instantly share code, notes, and snippets.

@hbhutta
Created March 17, 2025 00:17
Show Gist options
  • Select an option

  • Save hbhutta/29dc0f5f6b65d1fe6eb6c2b057de993a to your computer and use it in GitHub Desktop.

Select an option

Save hbhutta/29dc0f5f6b65d1fe6eb6c2b057de993a to your computer and use it in GitHub Desktop.
class Dequeue() {
/**
Construct a dequeue of the form:
B F
---------------------
e/d e/d
---------------------
*/
private queue: number[];
constructor() {
this.queue = [];
}
enqueueFront(v: number): void { // O(1)
this.queue.push(v);
}
enqueueBack(v: number): void { // O(1)
this.queue = [v].concat(this.queue);
}
dequeueFront(): number { // O(1)
return this.queue.pop();
}
dequeueBack(): number { // O(n)
this.queue.reverse();
let d = this.queue.pop();
this.queue.reverse();
return d;
}
peekBack(): number { // O(1)
return this.queue.at(0)
}
peekFront(): number { // O(1)
return this.queue.at(-1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment