Skip to content

Commit a04ab91

Browse files
authored
Merge branch 'CodeHarborHub:main' into main
2 parents f8cd4e3 + 729a834 commit a04ab91

File tree

205 files changed

+19956
-999
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

205 files changed

+19956
-999
lines changed

.github/workflows/greetings.yml

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,61 @@
1-
name: Greetings
1+
name: 'Greetings'
22

3-
on: [pull_request_target, issues]
3+
on:
4+
issues:
5+
types: [opened]
6+
pull_request_target:
7+
types: [opened]
8+
9+
permissions:
10+
issues: write
11+
pull-requests: write
412

513
jobs:
6-
greeting:
14+
welcome:
715
runs-on: ubuntu-latest
8-
permissions:
9-
issues: write
10-
pull-requests: write
16+
1117
steps:
18+
- name: Check out repository
19+
uses: actions/checkout@v4
20+
1221
- name: Greet first-time contributors
1322
id: greet
1423
uses: actions/first-interaction@v1
1524
with:
1625
repo-token: ${{ secrets.GITHUB_TOKEN }}
1726
issue-message: |
18-
Hi there! 👋 Thank you for opening your first issue on CodeHarborHub. We're excited to hear your thoughts and help you out. Please provide as much detail as you can so we can assist you effectively.
27+
Hi there! 👋 Thank you for opening your issue on CodeHarborHub. We're excited to hear your thoughts and help you out. You've raised a great topic! Please provide as much detail as you can so we can assist you effectively. Welcome aboard!
1928
pr-message: |
20-
Hi there! 👋 Thank you for submitting your first pull request to CodeHarborHub. We appreciate your contribution! Our team will review it soon. If you have any questions or need further assistance, feel free to reach out.
29+
Hi there! 👋 Thank you for submitting your pull request to CodeHarborHub. Great job on the contribution! 🎉 We appreciate your efforts and our team will review it soon. If you have any questions or need further assistance, feel free to reach out. Keep up the great work!
30+
31+
- name: Assign issue or pull request to team member
32+
if: github.event_name == 'issues' || github.event_name == 'pull_request_target'
33+
run: |
34+
ISSUE_NUMBER=${{ github.event.issue.number || github.event.pull_request.number }}
35+
curl -X POST -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
36+
-d '{"assignees":["team-member"]}' \
37+
"https://api.github.com/repos/${{ github.repository }}/issues/${ISSUE_NUMBER}"
38+
39+
- name: Welcome message for community contributors
40+
if: github.event_name == 'issues' || github.event_name == 'pull_request_target'
41+
uses: EddieHubCommunity/gh-action-community/src/welcome@main
42+
with:
43+
github-token: ${{ secrets.GITHUB_TOKEN }}
44+
issue-message: "Hi @${{ github.actor }}! Thanks for opening this issue. We appreciate your contribution to this open-source project. Your input is valuable and we aim to respond or assign your issue as soon as possible. Thanks again!"
45+
pr-message: "Great job, @${{ github.actor }}! 🎉 Thank you for submitting your pull request to CodeHarborHub. We appreciate your contribution and enthusiasm! Our team will review it soon. If you have any questions or need further assistance, feel free to reach out. Thanks for contributing!"
46+
47+
- name: Label first-time issues
48+
if: github.event_name == 'issues'
49+
run: |
50+
ISSUE_NUMBER=${{ github.event.issue.number }}
51+
curl -X POST -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
52+
-d '{"labels":["CodeHarborHub - Thanks for creating an issue!"]}' \
53+
"https://api.github.com/repos/${{ github.repository }}/issues/${ISSUE_NUMBER}"
54+
55+
- name: Label first-time pull requests
56+
if: github.event_name == 'pull_request_target'
57+
run: |
58+
PR_NUMBER=${{ github.event.pull_request.number }}
59+
curl -X POST -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
60+
-d '{"labels":["CodeHarborHub - Thanks for creating a pull request!"]}' \
61+
"https://api.github.com/repos/${{ github.repository }}/issues/${PR_NUMBER}"

622-design-circular-queue.md

Lines changed: 321 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,321 @@
1+
---
2+
id: design-circular-queue
3+
title: Design Circular Queue (Leetcode)
4+
sidebar_label: 0622-DesignCircularQueue
5+
description: Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle, and the last position is connected back to the first position to make a circle. It is also called "Ring Buffer".One of the benefits of the circular queue is that we can make use of the spaces in front of the queue. In a normal queue, once the queue becomes full, we cannot insert the next element even if there is a space in front of the queue. But using the circular queue, we can use the space to store new values.
6+
---
7+
8+
## Problem Description
9+
10+
| Problem Statement | Solution Link | LeetCode Profile |
11+
| :---------------- | :------------ | :--------------- |
12+
| [Design Circular Queue](https://leetcode.com/problems/design-circular-queue/description/) | [Design Circular Queue Solution on LeetCode](https://leetcode.com/problems/design-circular-queue/solutions) | [Aaradhya Singh ](https://leetcode.com/u/keira_09/) |
13+
14+
15+
## Problem Description
16+
17+
Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle, and the last position is connected back to the first position to make a circle. It is also called "Ring Buffer".
18+
19+
One of the benefits of the circular queue is that we can make use of the spaces in front of the queue. In a normal queue, once the queue becomes full, we cannot insert the next element even if there is a space in front of the queue. But using the circular queue, we can use the space to store new values.
20+
21+
Implement the $MyCircularQueue$ class:
22+
23+
- $MyCircularQueue(k)$ Initializes the object with the size of the queue to be k.
24+
- $int Front()$ Gets the front item from the queue. If the queue is empty, return -1.
25+
- $int Rear()$ Gets the last item from the queue. If the queue is empty, return -1.
26+
- $boolean enQueue(int value)$ Inserts an element into the circular queue. Return true if the operation is successful.
27+
- $boolean deQueue()$ Deletes an element from the circular queue. Return true if the operation is successful.
28+
- $boolean isEmpty()$ Checks whether the circular queue is empty or not.
29+
- $boolean isFull()$ Checks whether the circular queue is full or not.
30+
31+
You must solve the problem without using the built-in queue data structure in your programming language.
32+
33+
### Examples
34+
35+
#### Example 1
36+
37+
- **Input:** $["MyCircularQueue", "enQueue", "enQueue", "enQueue", "enQueue", "Rear", "isFull", "deQueue", "enQueue", "Rear"]$
38+
$[[3], [1], [2], [3], [4], [], [], [], [4], []]$
39+
- **Output:** $[null, true, true, true, false, 3, true, true, true, 4]$
40+
- **Explanation:**
41+
MyCircularQueue myCircularQueue = new MyCircularQueue(3); <br />
42+
myCircularQueue.enQueue(1); // return True
43+
myCircularQueue.enQueue(2); // return True
44+
myCircularQueue.enQueue(3); // return True
45+
myCircularQueue.enQueue(4); // return False
46+
myCircularQueue.Rear(); // return 3
47+
myCircularQueue.isFull(); // return True
48+
myCircularQueue.deQueue(); // return True
49+
myCircularQueue.enQueue(4); // return True
50+
myCircularQueue.Rear(); // return 4
51+
52+
53+
54+
### Constraints
55+
56+
- $1 <= k <= 1000$
57+
- $0 <= value <= 1000$
58+
- At most 3000 calls will be made to enQueue, deQueue, Front, Rear, isEmpty, and isFull.
59+
60+
61+
### Intuition
62+
63+
The code implements a circular queue, which is a data structure that efficiently manages a fixed-size queue with wrap-around capabilities. The circular queue is implemented using an array and maintains pointers for the front and rear of the queue, along with a counter to keep track of the number of elements. The enQueue function adds an element to the rear of the queue, adjusting the rear pointer to wrap around if necessary, while the deQueue function removes an element from the front, adjusting the front pointer similarly. The Front and Rear functions return the front and rear elements, respectively. The isEmpty and isFull functions check if the queue is empty or full based on the count. This design allows for efficient enqueue and dequeue operations while maintaining a fixed size and supporting wrap-around indexing.
64+
65+
### Approach
66+
67+
1. **Initialization:**
68+
69+
- The constructor MyCircularQueue(int k) initializes the circular queue with a fixed size k. It allocates memory for the array arr of size k, and initializes the front and rear pointers to -1 to indicate an empty queue. The count variable is also initialized to 0 to keep track of the number of elements in the queue.
70+
71+
2. **Enqueue Operation:**
72+
73+
- The enQueue(int value) method adds an element to the rear of the queue.
74+
- First, it checks if the queue is full by calling the isFull() method.
75+
- If the queue is full, it returns false.
76+
- If the queue is empty (checked using isEmpty()), it sets the front pointer to 0.
77+
- It then calculates the new position of the rear pointer using modulo operation (rear + 1) % size to handle wrap-around.
78+
- The element is added to the new rear position, and the count is incremented.
79+
- Finally, it returns true indicating the enqueue operation was successful.
80+
81+
3. **Dequeue Operation:**
82+
83+
- The deQueue() method removes an element from the front of the queue.
84+
- First, it checks if the queue is empty by calling the isEmpty() method.
85+
- If the queue is empty, it returns false.
86+
- If the front and rear pointers are equal, it means the queue will be empty after this operation, so both pointers are reset to -1.
87+
- Otherwise, it calculates the new position of the front pointer using modulo operation (front + 1) % size to handle wrap-around.
88+
- The count is decremented.
89+
- Finally, it returns true indicating the dequeue operation was successful.
90+
91+
4. **Front and Rear Operations:**
92+
93+
- The Front() method returns the element at the front of the queue.
94+
- It checks if the queue is empty using isEmpty().
95+
- If the queue is empty, it returns -1.
96+
- Otherwise, it returns the element at the front pointer.
97+
- The Rear() method returns the element at the rear of the queue.
98+
- It checks if the queue is empty using isEmpty().
99+
- If the queue is empty, it returns -1.
100+
- Otherwise, it returns the element at the rear pointer.
101+
102+
5. **Empty and Full Checks:**
103+
104+
- The isEmpty() method checks if the queue is empty by comparing the count to 0.
105+
- If count is 0, it returns true; otherwise, it returns false.
106+
- The isFull() method checks if the queue is full by comparing the count to the size of the queue.
107+
- If count is equal to size, it returns true; otherwise, it returns false.
108+
109+
### Solution Code
110+
111+
#### Python
112+
113+
```py
114+
class MyCircularQueue:
115+
def __init__(self, k: int):
116+
self.size = k
117+
self.arr = [0] * k
118+
self.front = -1
119+
self.rear = -1
120+
self.count = 0
121+
122+
def enQueue(self, value: int) -> bool:
123+
if self.isFull():
124+
return False
125+
126+
if self.isEmpty():
127+
self.front = 0
128+
129+
self.rear = (self.rear + 1) % self.size
130+
self.arr[self.rear] = value
131+
self.count += 1
132+
return True
133+
134+
def deQueue(self) -> bool:
135+
if self.isEmpty():
136+
return False
137+
138+
if self.front == self.rear:
139+
self.front = self.rear = -1
140+
else:
141+
self.front = (self.front + 1) % self.size
142+
143+
self.count -= 1
144+
return True
145+
146+
def Front(self) -> int:
147+
if self.isEmpty():
148+
return -1
149+
150+
return self.arr[self.front]
151+
152+
def Rear(self) -> int:
153+
if self.isEmpty():
154+
return -1
155+
156+
return self.arr[self.rear]
157+
158+
def isEmpty(self) -> bool:
159+
return self.count == 0
160+
161+
def isFull(self) -> bool:
162+
return self.count == self.size
163+
```
164+
165+
#### Java
166+
167+
```java
168+
class MyCircularQueue {
169+
private int[] arr;
170+
private int size;
171+
private int front;
172+
private int rear;
173+
private int count;
174+
175+
public MyCircularQueue(int k) {
176+
size = k;
177+
arr = new int[size];
178+
front = -1;
179+
rear = -1;
180+
count = 0;
181+
}
182+
183+
public boolean enQueue(int value) {
184+
if (isFull()) {
185+
return false;
186+
}
187+
188+
if (isEmpty()) {
189+
front = 0;
190+
}
191+
192+
rear = (rear + 1) % size;
193+
arr[rear] = value;
194+
count++;
195+
return true;
196+
}
197+
198+
public boolean deQueue() {
199+
if (isEmpty()) {
200+
return false;
201+
}
202+
203+
if (front == rear) {
204+
front = rear = -1;
205+
} else {
206+
front = (front + 1) % size;
207+
}
208+
209+
count--;
210+
return true;
211+
}
212+
213+
public int Front() {
214+
if (isEmpty()) {
215+
return -1;
216+
}
217+
218+
return arr[front];
219+
}
220+
221+
public int Rear() {
222+
if (isEmpty()) {
223+
return -1;
224+
}
225+
226+
return arr[rear];
227+
}
228+
229+
public boolean isEmpty() {
230+
return count == 0;
231+
}
232+
233+
public boolean isFull() {
234+
return count == size;
235+
}
236+
}
237+
```
238+
239+
#### C++
240+
241+
```cpp
242+
class MyCircularQueue {
243+
private:
244+
int* arr;
245+
int size;
246+
int front;
247+
int rear;
248+
int count;
249+
250+
public:
251+
MyCircularQueue(int k) {
252+
size = k;
253+
arr = new int[size];
254+
front = -1;
255+
rear = -1;
256+
count = 0;
257+
}
258+
259+
~MyCircularQueue() {
260+
delete[] arr;
261+
}
262+
263+
bool enQueue(int value) {
264+
if (isFull()) {
265+
return false; // Queue is full
266+
}
267+
268+
if (isEmpty()) {
269+
front = 0;
270+
}
271+
272+
rear = (rear + 1) % size;
273+
arr[rear] = value;
274+
count++;
275+
return true;
276+
}
277+
278+
bool deQueue() {
279+
if (isEmpty()) {
280+
return false; // Queue is empty
281+
}
282+
283+
if (front == rear) {
284+
front = rear = -1;
285+
} else {
286+
front = (front + 1) % size;
287+
}
288+
289+
count--;
290+
return true;
291+
}
292+
293+
int Front() {
294+
if (isEmpty()) {
295+
return -1; // Queue is empty
296+
}
297+
298+
return arr[front];
299+
}
300+
301+
int Rear() {
302+
if (isEmpty()) {
303+
return -1; // Queue is empty
304+
}
305+
306+
return arr[rear];
307+
}
308+
309+
bool isEmpty() {
310+
return count == 0;
311+
}
312+
313+
bool isFull() {
314+
return count == size;
315+
}
316+
};
317+
```
318+
319+
### Conclusion
320+
321+
The provided code implementation of a circular queue offers efficient enqueue, dequeue, and access operations with a fixed-size capacity. Enqueue and dequeue operations have a time complexity of $O(1)$, as they involve updating pointers and array accesses. Accessing the front or rear element also has a time complexity of $O(1)$. The space complexity of the circular queue is $O(n)$, where $n$ is the fixed size of the queue, as it uses an array to store the elements. Overall, this implementation provides a convenient and performant solution for managing a circular queue with constant-time complexity for core operations and linear space complexity.

0 commit comments

Comments
 (0)