diff --git a/leetcode/problems/cpp/design-circular-queue.cpp b/leetcode/problems/cpp/design-circular-queue.cpp new file mode 100644 index 0000000..17c252c --- /dev/null +++ b/leetcode/problems/cpp/design-circular-queue.cpp @@ -0,0 +1,55 @@ +// by @codeAbinash + +#include "vector" +using namespace std; + +class MyCircularQueue { + private: + vector data; + int size, front = -1, rear = -1; + + public: + MyCircularQueue(int k) { + data.resize(k); + size = k; + } + + bool enQueue(int value) { + if (isFull()) return false; + if (isEmpty()) { + rear = front = 0; + data[rear] = value; + return true; + } + rear = (rear + 1) % size; + data[rear] = value; + return true; + } + + bool deQueue() { + if (isEmpty()) return false; + if (front == rear) { + front = rear = -1; + return true; + } + front = (front + 1) % size; + return true; + } + + int Front() { + if (isEmpty()) return -1; + return data[front]; + } + + int Rear() { + if (isEmpty()) return -1; + return data[rear]; + } + + bool isEmpty() { + if (front == -1 && rear == -1) return true; + return false; + } + + bool isFull() { return (rear + 1) % size == front; } +}; diff --git a/leetcode/problems/cpp/maximum-score-from-removing-stones.cpp b/leetcode/problems/cpp/maximum-score-from-removing-stones.cpp new file mode 100644 index 0000000..b4b7d81 --- /dev/null +++ b/leetcode/problems/cpp/maximum-score-from-removing-stones.cpp @@ -0,0 +1,35 @@ +// by @codeAbinash +// Time : O(n) +// Space : O(n) + +#include "queue" +using namespace std; + +class Solution { + public: + int maximumScore(int a, int b, int c) { + if (a < b) return maximumScore(b, a, c); + if (b < c) return maximumScore(a, c, b); + return b == 0 ? 0 : 1 + maximumScore(a - 1, b - 1, c); + } +}; + +// Using priority queue +class Solution2 { + public: + int maximumScore(int a, int b, int c) { + priority_queue pq; + pq.push(a); + pq.push(b); + pq.push(c); + int ans = 0; + while (pq.size() > 1) { + int x = pq.top(); pq.pop(); + int y = pq.top(); pq.pop(); + if (--x) pq.push(x); + if (--y) pq.push(y); + ans++; + } + return ans; + } +};