Skip to content

Latest commit

 

History

History
208 lines (181 loc) · 3.75 KB

design_circular_queue.md

File metadata and controls

208 lines (181 loc) · 3.75 KB

🔥 Dart 🔥| simple fast easy and linear solution

Solution - 1

classMyCircularQueue { // Runtime: 512 ms, faster than 100.00% of Dart online submissions for Design Circular Queue.// Memory Usage: 151.6 MB, less than 50.00% of Dart online submissions for Design Circular Queue.// MyCircularQueuelateList<int> a; int front =0; int rear =-1; int len =0; MyCircularQueue(int k) { this.a = [k]; a =List.filled(k, 0); this.front; this.rear; this.len; } boolenQueue(int value) { if (!isFull()) { if (++rear == a.length) rear =0; a[rear] = value; len++; returntrue; } elsereturnfalse; } booldeQueue() { if (!isEmpty()) { if (++front == a.length) front =0; len--; returntrue; } elsereturnfalse; } intFront() { returnisEmpty() ?-1: a[front]; } intRear() { returnisEmpty() ?-1: a[rear]; } boolisEmpty() { return len ==0; } boolisFull() { return len == a.length; } }

Solution - 2

classMyCircularQueue { // Runtime: 620 ms, faster than 50.00% of Dart online submissions for Design Circular Queue.// Memory Usage: 151.7 MB, less than 50.00% of Dart online submissions for Design Circular Queue.int front =-1; int rear =-1; lateList<int> arr; int cap =0; MyCircularQueue(int k) { arr =List.filled(k, 0); front; cap = k; rear; } intnext(int i) { // to get next idx after i in circular queuereturn (i +1) % cap; } intprev(int i) { // to get prev idx before i in circular queuereturn (i + cap -1) % cap; } boolenQueue(int value) { if (isFull()) returnfalse; if (front ==-1) { front =0; rear =0; arr[rear] = value; returntrue; } rear =next(rear); arr[rear] = value; returntrue; } booldeQueue() { if (isEmpty()) returnfalse; if (front == rear) { front =-1; rear =-1; returntrue; } front =next(front); returntrue; } intFront() { if (front ==-1) return-1; return arr[front]; } intRear() { if (rear ==-1) return-1; return arr[rear]; } boolisEmpty() { return front ==-1; } boolisFull() { return front !=-1&&next(rear) == front; } }

Solution - 3 Using LinkedList

classListNode { lateint val; ListNode? prev, next; ListNode(int x) { val = x; prev =null; next =null; } }
classMyCircularQueue { // Runtime: 644 ms, faster than 50.00% of Dart online submissions for Design Circular Queue.// Memory Usage: 153.5 MB, less than 50.00% of Dart online submissions for Design Circular Queue.lateint queueSize, currSize; lateListNode head, tail; // MyCircularQueueMyCircularQueue(int k) { queueSize = k; currSize =0; head =ListNode(-1); tail =ListNode(-1); head.next = tail; tail.prev = head; } boolenQueue(int value) { if (isFull()) { returnfalse; } ListNode newNode =newListNode(value); newNode.next = tail; newNode.prev = tail.prev; tail.prev!.next = newNode; tail.prev = newNode; currSize++; returntrue; } booldeQueue() { if (isEmpty()) { returnfalse; } ListNode? toBeDeleted = head.next; head.next = toBeDeleted!.next; toBeDeleted.next!.prev = head; toBeDeleted.next =null; toBeDeleted.prev =null; currSize--; returntrue; } intFront() { if (isEmpty()) { return-1; } return head.next!.val; } intRear() { if (isEmpty()) { return-1; } return tail.prev!.val; } boolisEmpty() { return currSize ==0; } boolisFull() { return currSize == queueSize; } } 
close