leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit e32bf0d88aba53d78893d811d8fb693db2751f05
parent 0e1dfd353f8bc5651de0d118869a6e534bba79c4
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Wed, 22 May 2024 19:37:36 +0200

1 Random Problem

Diffstat:
AProblems/0622.cpp | 44++++++++++++++++++++++++++++++++++++++++++++
MREADME.md | 1+
2 files changed, 45 insertions(+), 0 deletions(-)

diff --git a/Problems/0622.cpp b/Problems/0622.cpp @@ -0,0 +1,44 @@ +class MyCircularQueue { + public: + MyCircularQueue(int k) : n(k), q(k) {} + + bool enQueue(int value) { + if (isFull()) return false; + + q[back++] = value; + if (back >= n) back -= n; + size++; + + return true; + } + + bool deQueue() { + if (isEmpty()) return false; + + if (++front >= n) front -= n; + size--; + + return true; + } + + int Front() const { + if (isEmpty()) return -1; + return q[front]; + } + + int Rear() const { + if (isEmpty()) return -1; + if (back == 0) return q.back(); + return q[back - 1]; + } + + bool isEmpty() const { return size == 0; } + + bool isFull() const { return size == n; } + + const int n; + vector<int> q; + + int front = 0, back = 0; + int size = 0; +}; diff --git a/README.md b/README.md @@ -418,6 +418,7 @@ for solving problems. | 0619 | Easy | [Biggest Single Number](Problems/0619.cpp) | | 0620 | Easy | [Not Boring Movies](Problems/0620.cpp) | | 0621 | Medium | [Task Scheduler](Problems/0621.cpp) | +| 0622 | Medium | [Design Circular Queue](Problems/0622.cpp) | | 0623 | Medium | [Add One Row to Tree](Problems/0623.cpp) | | 0626 | Medium | [Exchange Seats](Problems/0626.cpp) | | 0627 | Easy | [Swap Salary](Problems/0627.cpp) |