LeetCode: Design Circular Queue Solution

1/**
2 * @param {number} k
3 */
4var MyCircularQueue = function (k) {
5 this.arr = []
6 this.maxSize = k
7}
8
9/**
10 * @param {number} value
11 * @return {boolean}
12 */
13MyCircularQueue.prototype.enQueue = function (value) {
14 if (this.isFull()) return false
15 this.arr.push(value)
16 return true
17}
18
19/**
20 * @return {boolean}
21 */
22MyCircularQueue.prototype.deQueue = function () {
23 if (this.isEmpty()) return false
24 this.arr.shift()
25 return true
26}
27
28/**
29 * @return {number}
30 */
31MyCircularQueue.prototype.Front = function () {
32 return this.isEmpty() ? -1 : this.arr[0]
33}
34
35/**
36 * @return {number}
37 */
38MyCircularQueue.prototype.Rear = function () {
39 return this.isEmpty() ? -1 : this.arr.slice(-1)[0]
40}
41
42/**
43 * @return {boolean}
44 */
45MyCircularQueue.prototype.isEmpty = function () {
46 return this.arr.length === 0
47}
48
49/**
50 * @return {boolean}
51 */
52MyCircularQueue.prototype.isFull = function () {
53 return this.arr.length === this.maxSize
54}
55
56/**
57 * Your MyCircularQueue object will be instantiated and called as such:
58 * var obj = new MyCircularQueue(k)
59 * var param_1 = obj.enQueue(value)
60 * var param_2 = obj.deQueue()
61 * var param_3 = obj.Front()
62 * var param_4 = obj.Rear()
63 * var param_5 = obj.isEmpty()
64 * var param_6 = obj.isFull()
65 */

Comments

Loading comments...

Tags

leetcode

queue

Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.

Next Post

LeetCode: Global And Local Inversions

Apr 6, 2021

Previous Post

HoningJS

Search Posts