Skip to content

Commit 7059302

Browse files
committed
leetcode
1 parent 5d42127 commit 7059302

File tree

4 files changed

+607
-0
lines changed

4 files changed

+607
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
/*
2+
3+
4+
-* Design a Circle Queue *-
5+
6+
7+
8+
Design your implementation of the circular queue.
9+
The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out)
10+
principle and the last position is connected back to the first position to make a circle. It is also called "Ring Buffer".
11+
12+
One of the benefits of the circular queue is that we can make use of the spaces in front of the queue.
13+
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.
14+
But using the circular queue, we can use the space to store new values.
15+
16+
Implementation the MyCircularQueue class:
17+
18+
MyCircularQueue(k) Initializes the object with the size of the queue to be k.
19+
int Front() Gets the front item from the queue. If the queue is empty, return -1.
20+
int Rear() Gets the last item from the queue. If the queue is empty, return -1.
21+
boolean enQueue(int value) Inserts an element into the circular queue. Return true if the operation is successful.
22+
boolean deQueue() Deletes an element from the circular queue. Return true if the operation is successful.
23+
boolean isEmpty() Checks whether the circular queue is empty or not.
24+
boolean isFull() Checks whether the circular queue is full or not.
25+
You must solve the problem without using the built-in queue data structure in your programming language.
26+
27+
28+
29+
Example 1:
30+
31+
Input
32+
["MyCircularQueue", "enQueue", "enQueue", "enQueue", "enQueue", "Rear", "isFull", "deQueue", "enQueue", "Rear"]
33+
[[3], [1], [2], [3], [4], [], [], [], [4], []]
34+
Output
35+
[null, true, true, true, false, 3, true, true, true, 4]
36+
37+
Explanation
38+
MyCircularQueue myCircularQueue = new MyCircularQueue(3);
39+
myCircularQueue.enQueue(1); // return True
40+
myCircularQueue.enQueue(2); // return True
41+
myCircularQueue.enQueue(3); // return True
42+
myCircularQueue.enQueue(4); // return False
43+
myCircularQueue.Rear(); // return 3
44+
myCircularQueue.isFull(); // return True
45+
myCircularQueue.deQueue(); // return True
46+
myCircularQueue.enQueue(4); // return True
47+
myCircularQueue.Rear(); // return 4
48+
49+
50+
Constraints:
51+
52+
1 <= k <= 1000
53+
0 <= value <= 1000
54+
At most 3000 calls will be made to enQueue, deQueue, Front, Rear, isEmpty, and isFull.
55+
56+
57+
58+
59+
*/
60+
61+
/*
62+
*
63+
* Your MyCircularQueue object will be instantiated and called as such:
64+
* MyCircularQueue obj = MyCircularQueue(k);
65+
* bool param1 = obj.enQueue(value);
66+
* bool param2 = obj.deQueue();
67+
* int param3 = obj.Front();
68+
* int param4 = obj.Rear();
69+
* bool param5 = obj.isEmpty();
70+
* bool param6 = obj.isFull();
71+
*
72+
*/
73+
74+
class MyCircularQueue {
75+
// Runtime: 512 ms, faster than 100.00% of Dart online submissions for Design Circular Queue.
76+
// Memory Usage: 151.6 MB, less than 50.00% of Dart online submissions for Design Circular Queue.
77+
// MyCircularQueue
78+
late List<int> a;
79+
int front = 0;
80+
int rear = -1;
81+
int len = 0;
82+
MyCircularQueue(int k) {
83+
this.a = [k];
84+
a = List.filled(k, 0);
85+
this.front;
86+
this.rear;
87+
this.len;
88+
}
89+
90+
bool enQueue(int value) {
91+
if (!isFull()) {
92+
if (++rear == a.length) rear = 0;
93+
a[rear] = value;
94+
len++;
95+
return true;
96+
} else
97+
return false;
98+
}
99+
100+
bool deQueue() {
101+
if (!isEmpty()) {
102+
if (++front == a.length) front = 0;
103+
len--;
104+
return true;
105+
} else
106+
return false;
107+
}
108+
109+
int Front() {
110+
return isEmpty() ? -1 : a[front];
111+
}
112+
113+
int Rear() {
114+
return isEmpty() ? -1 : a[rear];
115+
}
116+
117+
bool isEmpty() {
118+
return len == 0;
119+
}
120+
121+
bool isFull() {
122+
return len == a.length;
123+
}
124+
}
125+
126+
class MyCircularQueue1 {
127+
// Runtime: 620 ms, faster than 50.00% of Dart online submissions for Design Circular Queue.
128+
// Memory Usage: 151.7 MB, less than 50.00% of Dart online submissions for Design Circular Queue.
129+
int front = -1;
130+
int rear = -1;
131+
late List<int> arr;
132+
int cap = 0;
133+
MyCircularQueue1(int k) {
134+
arr = List.filled(k, 0);
135+
front;
136+
cap = k;
137+
rear;
138+
}
139+
int next(int i) {
140+
// to get next idx after i in circular queue
141+
return (i + 1) % cap;
142+
}
143+
144+
int prev(int i) {
145+
// to get prev idx before i in circular queue
146+
return (i + cap - 1) % cap;
147+
}
148+
149+
bool enQueue(int value) {
150+
if (isFull()) return false;
151+
if (front == -1) {
152+
front = 0;
153+
rear = 0;
154+
arr[rear] = value;
155+
return true;
156+
}
157+
rear = next(rear);
158+
arr[rear] = value;
159+
return true;
160+
}
161+
162+
bool deQueue() {
163+
if (isEmpty()) return false;
164+
if (front == rear) {
165+
front = -1;
166+
rear = -1;
167+
return true;
168+
}
169+
front = next(front);
170+
return true;
171+
}
172+
173+
int Front() {
174+
if (front == -1) return -1;
175+
return arr[front];
176+
}
177+
178+
int Rear() {
179+
if (rear == -1) return -1;
180+
return arr[rear];
181+
}
182+
183+
bool isEmpty() {
184+
return front == -1;
185+
}
186+
187+
bool isFull() {
188+
return front != -1 && next(rear) == front;
189+
}
190+
}
191+
192+
class ListNode {
193+
late int val;
194+
ListNode? prev, next;
195+
ListNode(int x) {
196+
val = x;
197+
prev = null;
198+
next = null;
199+
}
200+
}
201+
202+
class C {
203+
// Runtime: 644 ms, faster than 50.00% of Dart online submissions for Design Circular Queue.
204+
// Memory Usage: 153.5 MB, less than 50.00% of Dart online submissions for Design Circular Queue.
205+
late int queueSize, currSize;
206+
late ListNode head, tail;
207+
// MyCircularQueue
208+
C(int k) {
209+
queueSize = k;
210+
currSize = 0;
211+
head = ListNode(-1);
212+
tail = ListNode(-1);
213+
head.next = tail;
214+
tail.prev = head;
215+
}
216+
217+
bool enQueue(int value) {
218+
if (isFull()) {
219+
return false;
220+
}
221+
ListNode newNode = new ListNode(value);
222+
newNode.next = tail;
223+
newNode.prev = tail.prev;
224+
tail.prev!.next = newNode;
225+
tail.prev = newNode;
226+
currSize++;
227+
return true;
228+
}
229+
230+
bool deQueue() {
231+
if (isEmpty()) {
232+
return false;
233+
}
234+
ListNode? toBeDeleted = head.next;
235+
head.next = toBeDeleted!.next;
236+
toBeDeleted.next!.prev = head;
237+
toBeDeleted.next = null;
238+
toBeDeleted.prev = null;
239+
currSize--;
240+
return true;
241+
}
242+
243+
int Front() {
244+
if (isEmpty()) {
245+
return -1;
246+
}
247+
return head.next!.val;
248+
}
249+
250+
int Rear() {
251+
if (isEmpty()) {
252+
return -1;
253+
}
254+
return tail.prev!.val;
255+
}
256+
257+
bool isEmpty() {
258+
return currSize == 0;
259+
}
260+
261+
bool isFull() {
262+
return currSize == queueSize;
263+
}
264+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package main
2+
3+
// type MyCircularQueue struct {
4+
// slice []int
5+
// front, rear, size int
6+
// }
7+
8+
// func Constructor(k int) MyCircularQueue {
9+
// return MyCircularQueue{
10+
// size: k,
11+
// slice: make([]int, k),
12+
// front: 0,
13+
// rear: -1,
14+
// }
15+
// }
16+
17+
// func (q *MyCircularQueue) EnQueue(value int) bool {
18+
// if q.IsFull() {
19+
// return false
20+
// }
21+
// q.rear++
22+
// q.slice[q.rear%q.size] = value
23+
// return true
24+
// }
25+
26+
// func (q *MyCircularQueue) DeQueue() bool {
27+
// if q.IsEmpty() {
28+
// return false
29+
// }
30+
// q.front++
31+
// return true
32+
// }
33+
34+
// func (q *MyCircularQueue) Front() int {
35+
// if q.IsEmpty() {
36+
// return -1
37+
// }
38+
// return q.slice[q.front%q.size]
39+
// }
40+
41+
// func (q *MyCircularQueue) Rear() int {
42+
// if q.IsEmpty() {
43+
// return -1
44+
// }
45+
// return q.slice[q.rear%q.size]
46+
// }
47+
48+
// func (q *MyCircularQueue) IsEmpty() bool {
49+
// return q.rear < q.front
50+
// }
51+
52+
// func (q *MyCircularQueue) IsFull() bool {
53+
// return q.rear-q.front == q.size-1
54+
// }
55+
56+
type MyCircularQueue struct {
57+
queue []int
58+
front int
59+
rear int
60+
size int
61+
}
62+
63+
/** Initialize your data structure here. Set the size of the queue to be k. */
64+
func Constructor(k int) MyCircularQueue {
65+
return MyCircularQueue{
66+
queue: make([]int, k),
67+
front: -1,
68+
rear: -1,
69+
size: k,
70+
}
71+
}
72+
73+
/** Insert an element into the circular queue. Return true if the operation is successful. */
74+
func (this *MyCircularQueue) EnQueue(value int) bool {
75+
if this.IsFull() {
76+
return false
77+
}
78+
79+
this.rear = (this.rear + 1) % this.size
80+
if this.front == -1 {
81+
this.front = this.rear
82+
}
83+
this.queue[this.rear] = value
84+
return true
85+
}
86+
87+
/** Delete an element from the circular queue. Return true if the operation is successful. */
88+
func (this *MyCircularQueue) DeQueue() bool {
89+
if this.IsEmpty() {
90+
return false
91+
}
92+
93+
if this.rear == this.front {
94+
this.rear = -1
95+
this.front = -1
96+
} else {
97+
this.front = (this.front + 1) % this.size
98+
}
99+
100+
return true
101+
}
102+
103+
/** Get the front item from the queue. */
104+
func (this *MyCircularQueue) Front() int {
105+
if this.IsEmpty() {
106+
return -1
107+
}
108+
return this.queue[this.front]
109+
}
110+
111+
/** Get the last item from the queue. */
112+
func (this *MyCircularQueue) Rear() int {
113+
if this.IsEmpty() {
114+
return -1
115+
}
116+
return this.queue[this.rear]
117+
}
118+
119+
/** Checks whether the circular queue is empty or not. */
120+
func (this *MyCircularQueue) IsEmpty() bool {
121+
if this.front == -1 && this.rear == -1 {
122+
return true
123+
}
124+
return false
125+
}
126+
127+
/** Checks whether the circular queue is full or not. */
128+
func (this *MyCircularQueue) IsFull() bool {
129+
if this.front == (this.rear+1)%this.size {
130+
return true
131+
}
132+
return false
133+
}

0 commit comments

Comments
 (0)