Skip to content

Commit 2701840

Browse files
committedMar 7, 2025
Add solution #622
1 parent 77ef1b5 commit 2701840

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@
471471
611|[Valid Triangle Number](./0611-valid-triangle-number.js)|Medium|
472472
617|[Merge Two Binary Trees](./0617-merge-two-binary-trees.js)|Easy|
473473
621|[Task Scheduler](./0621-task-scheduler.js)|Medium|
474+
622|[Design Circular Queue](./0622-design-circular-queue.js)|Medium|
474475
628|[Maximum Product of Three Numbers](./0628-maximum-product-of-three-numbers.js)|Easy|
475476
637|[Average of Levels in Binary Tree](./0637-average-of-levels-in-binary-tree.js)|Easy|
476477
643|[Maximum Average Subarray I](./0643-maximum-average-subarray-i.js)|Easy|
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* 622. Design Circular Queue
3+
* https://leetcode.com/problems/design-circular-queue/
4+
* Difficulty: Medium
5+
*
6+
* Design your implementation of the circular queue. The circular queue is a linear data structure
7+
* in which the operations are performed based on FIFO (First In First Out) principle, and the
8+
* last position is connected back to the first position to make a circle. It is also called
9+
* "Ring Buffer".
10+
*
11+
* One of the benefits of the circular queue is that we can make use of the spaces in front of
12+
* the queue. In a normal queue, once the queue becomes full, we cannot insert the next element
13+
* even if there is a space in front of the queue. But using the circular queue, we can use the
14+
* space to store new values.
15+
*
16+
* Implement the MyCircularQueue class:
17+
* - MyCircularQueue(k) Initializes the object with the size of the queue to be k.
18+
* - int Front() Gets the front item from the queue. If the queue is empty, return -1.
19+
* - int Rear() Gets the last item from the queue. If the queue is empty, return -1.
20+
* - boolean enQueue(int value) Inserts an element into the circular queue. Return true if the
21+
* operation is successful.
22+
* - boolean deQueue() Deletes an element from the circular queue. Return true if the operation
23+
* is successful.
24+
* - boolean isEmpty() Checks whether the circular queue is empty or not.
25+
* - boolean isFull() Checks whether the circular queue is full or not.
26+
*
27+
* You must solve the problem without using the built-in queue data structure in your programming
28+
* language.
29+
*/
30+
31+
/**
32+
* @param {number} k
33+
*/
34+
var MyCircularQueue = function(k) {
35+
this.queue = new Array(k);
36+
this.size = k;
37+
this.front = -1;
38+
this.rear = -1;
39+
};
40+
41+
/**
42+
* @param {number} value
43+
* @return {boolean}
44+
*/
45+
MyCircularQueue.prototype.enQueue = function(value) {
46+
if (this.isFull()) {
47+
return false;
48+
}
49+
if (this.isEmpty()) {
50+
this.front = 0;
51+
}
52+
this.rear = (this.rear + 1) % this.size;
53+
this.queue[this.rear] = value;
54+
return true;
55+
};
56+
57+
/**
58+
* @return {boolean}
59+
*/
60+
MyCircularQueue.prototype.deQueue = function() {
61+
if (this.isEmpty()) {
62+
return false;
63+
} else if (this.front === this.rear) {
64+
this.front = -1;
65+
this.rear = -1;
66+
} else {
67+
this.front = (this.front + 1) % this.size;
68+
}
69+
return true;
70+
};
71+
72+
/**
73+
* @return {number}
74+
*/
75+
MyCircularQueue.prototype.Front = function() {
76+
return this.isEmpty() ? -1 : this.queue[this.front];
77+
};
78+
79+
/**
80+
* @return {number}
81+
*/
82+
MyCircularQueue.prototype.Rear = function() {
83+
return this.isEmpty() ? -1 : this.queue[this.rear];
84+
};
85+
86+
/**
87+
* @return {boolean}
88+
*/
89+
MyCircularQueue.prototype.isEmpty = function() {
90+
return this.front === -1;
91+
};
92+
93+
/**
94+
* @return {boolean}
95+
*/
96+
MyCircularQueue.prototype.isFull = function() {
97+
return (this.rear + 1) % this.size === this.front;
98+
};

0 commit comments

Comments
 (0)
Please sign in to comment.