Leetcode 622: Design Circular Queue
Design and implement a Circular Queue. A circular queue is a linear data structure where the operations follow the FIFO principle, and the last position is connected to the first, forming a circle. This design allows better space utilization.
Problem
Approach
Steps
Complexity
Input: You will be provided with an integer `k`, the size of the circular queue. The operations available are enQueue, deQueue, Front, Rear, isEmpty, and isFull.
Example: ['MyCircularQueue', 'enQueue', 'enQueue', 'deQueue', 'Front', 'Rear', 'isEmpty', 'isFull'] [[5], [1], [2], [], [], [], [], []]
Constraints:
• 1 <= k <= 1000
• 0 <= value <= 1000
• At most 3000 calls will be made to enQueue, deQueue, Front, Rear, isEmpty, and isFull.
Output: For each operation, return the appropriate result: true/false for enQueue, deQueue, isEmpty, isFull; the element for Front and Rear operations.
Example: [null, true, true, true, false, 3, true, true, true, 4]
Constraints:
• Return true if the operation is successful, otherwise false.
• Return the front or rear element, or -1 if the queue is empty.
Goal: Implement a circular queue that efficiently reuses space when elements are dequeued.
Steps:
• 1. Initialize the queue with size k.
• 2. Implement methods for enQueue, deQueue, Front, Rear, isEmpty, and isFull.
• 3. Use modular arithmetic to handle the circular behavior of the queue.
Goal: The queue size and element values are bounded, and the total number of operations will not exceed the given limits.
Steps:
• 1 <= k <= 1000
• 0 <= value <= 1000
• At most 3000 calls will be made to the operations.
Assumptions:
• The operations will be called within the given constraints.
• Input: ['MyCircularQueue', 'enQueue', 'enQueue', 'enQueue', 'enQueue', 'Rear', 'isFull', 'deQueue', 'enQueue', 'Rear']
• Explanation: This example demonstrates the behavior of the queue when full, showing the operations enQueue, deQueue, and the correct handling of the queue size limit.
Approach: We will use a circular array with two pointers to track the front and rear of the queue, ensuring efficient insertion and removal of elements.
Observations:
• Circular queues provide better space utilization by reusing the front space once elements are dequeued.
• We will use modular arithmetic to wrap the indices around when the front or rear reaches the end of the array.
Steps:
• 1. Create a circular array of size `k`.
• 2. Implement a variable to keep track of the front and rear positions.
• 3. Use modular arithmetic to handle wrapping around the queue when inserting and removing elements.
Empty Inputs:
• Trying to dequeue or get the front/rear of an empty queue should return -1.
Large Inputs:
• The queue size can go up to 1000, but the implementation should still be efficient.
Special Values:
• When the queue is full, trying to enqueue an element should return false.
Constraints:
• Ensure the number of operations does not exceed 3000.
int frd, bck;
int sz, cnt;
public:
MyCircularQueue(int k): sz(k), frd(0), bck(-1), q(k, 0), cnt(0) {
}
bool enQueue(int value) {
if(isFull()) return false;
bck = (bck + 1) % sz;
q[bck] = value;
cnt++;
return true;
}
bool deQueue() {
if(isEmpty()) return false;
frd = (frd +1) %sz;
cnt--;
return true;
}
int Front() {
if(isEmpty()) return -1;
return q[frd];
}
int Rear() {
if(isEmpty()) return -1;
return q[bck];
}
bool isEmpty() {
return cnt == 0;
}
bool isFull() {
return cnt == sz;
}
};
/**
* Your MyCircularQueue object will be instantiated and called as such:
* MyCircularQueue* obj = new MyCircularQueue(k);
* bool param_1 = obj->enQueue(value);
* bool param_2 = obj->deQueue();
* int param_3 = obj->Front();
* int param_4 = obj->Rear();
* bool param_5 = obj->isEmpty();
* bool param_6 = obj->isFull();
1 : Variable Declarations
int frd, bck;
Declares two integer variables, `frd` (front) and `bck` (back), which track the front and back indices of the circular queue.
2 : Variable Declarations
int sz, cnt;
Declares two integer variables, `sz` (size) for the capacity of the queue and `cnt` (count) for the current number of elements in the queue.
3 : Constructor Declaration
public:
Marks the start of the public member functions for the `MyCircularQueue` class.
4 : Constructor Implementation
MyCircularQueue(int k): sz(k), frd(0), bck(-1), q(k, 0), cnt(0) {
Constructor that initializes the circular queue with a given size `k`, setting the front index (`frd`) to 0, the back index (`bck`) to -1, and initializing the queue array `q` with `k` zeros. The element count (`cnt`) is also set to 0.
5 : Enqueue Method
bool enQueue(int value) {
Defines the `enQueue` method that adds an element to the circular queue if it's not full.
6 : Enqueue Check Full
if(isFull()) return false;
Checks if the queue is full. If true, returns `false` to indicate the enqueue operation failed.
7 : Enqueue Update Back Index
bck = (bck + 1) % sz;
Updates the back index (`bck`) to the next position in the circular queue, wrapping around if necessary.
8 : Enqueue Insert Value
q[bck] = value;
Inserts the given value into the queue at the current back index.
9 : Enqueue Increment Count
cnt++;
Increments the count (`cnt`) of elements in the queue after successfully inserting the value.
10 : Enqueue Return
return true;
Returns `true` to indicate that the enqueue operation was successful.
11 : Dequeue Method
bool deQueue() {
Defines the `deQueue` method that removes an element from the front of the circular queue if it's not empty.
12 : Dequeue Check Empty
if(isEmpty()) return false;
Checks if the queue is empty. If true, returns `false` to indicate the dequeue operation failed.
13 : Dequeue Update Front Index
frd = (frd + 1) % sz;
Updates the front index (`frd`) to the next position in the circular queue, wrapping around if necessary.
14 : Dequeue Decrement Count
cnt--;
Decrements the count (`cnt`) of elements in the queue after successfully removing an element.
15 : Dequeue Return
return true;
Returns `true` to indicate that the dequeue operation was successful.
16 : Front Method
int Front() {
Defines the `Front` method that returns the element at the front of the circular queue, or -1 if empty.
17 : Front Check Empty
if(isEmpty()) return -1;
Checks if the queue is empty. If true, returns `-1` to indicate there is no front element.
18 : Front Return
return q[frd];
Returns the element at the front of the queue.
19 : Rear Method
int Rear() {
Defines the `Rear` method that returns the element at the rear of the circular queue, or -1 if empty.
20 : Rear Check Empty
if(isEmpty()) return -1;
Checks if the queue is empty. If true, returns `-1` to indicate there is no rear element.
21 : Rear Return
return q[bck];
Returns the element at the rear of the queue.
22 : isEmpty Method
bool isEmpty() {
Defines the `isEmpty` method that checks if the queue is empty.
23 : isEmpty Return
return cnt == 0;
Returns `true` if the queue is empty, i.e., if the element count (`cnt`) is 0.
24 : isFull Method
bool isFull() {
Defines the `isFull` method that checks if the queue is full.
25 : isFull Return
return cnt == sz;
Returns `true` if the queue is full, i.e., if the element count (`cnt`) is equal to the queue size (`sz`).
Best Case: O(1)
Average Case: O(1)
Worst Case: O(1)
Description: All operations (enQueue, deQueue, Front, Rear, isEmpty, isFull) take constant time.
Best Case: O(k)
Worst Case: O(k)
Description: Space complexity is proportional to the size of the queue, which is `k`.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus