Let's make this one less mysterious. For 622. Design Circular Queue, the solution in this repository is mainly a data structure design solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: data structure design.
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are enQueue, deQueue, Front, Rear, isEmpty.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 48 ms, faster than 8.99% of C++ online submissions for Design Circular Queue.
02//Memory Usage: 14 MB, less than 100.00% of C++ online submissions for Design Circular Queue.
03class MyCircularQueue {
04public:
05 vector<int> q;
06 int head, curSize;
07
08 /** Initialize your data structure here. Set the size of the queue to be k. */
09 MyCircularQueue(int k) {
10 q = vector<int>(k, -1);
11 head = 0; //head is inclusive
12 curSize = 0;
13 }
14
15 /** Insert an element into the circular queue. Return true if the operation is successful. */
16 bool enQueue(int value) {
17 if(isFull()) return false;
18 //head+curSize: the space after rear
19 q[(head+curSize)%q.size()] = value;
20 curSize++;
21 return true;
22 }
23
24 /** Delete an element from the circular queue. Return true if the operation is successful. */
25 bool deQueue() {
26 if(isEmpty()) return false;
27 q[head++] = -1;
28 curSize--;
29 return true;
30 }
31
32 /** Get the front item from the queue. */
33 int Front() {
34 return q[head];
35 }
36
37 /** Get the last item from the queue. */
38 int Rear() {
39 return q[(head+curSize-1)%q.size()];
40 }
41
42 /** Checks whether the circular queue is empty or not. */
43 bool isEmpty() {
44 return curSize == 0;
45 }
46
47 /** Checks whether the circular queue is full or not. */
48 bool isFull() {
49 return curSize == q.size();
50 }
51};
52
53/**
54 * Your MyCircularQueue object will be instantiated and called as such:
55 * MyCircularQueue* obj = new MyCircularQueue(k);
56 * bool param_1 = obj->enQueue(value);
57 * bool param_2 = obj->deQueue();
58 * int param_3 = obj->Front();
59 * int param_4 = obj->Rear();
60 * bool param_5 = obj->isEmpty();
61 * bool param_6 = obj->isFull();
62 */
Cost