Skip to content

Commit

Permalink
Added some solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
codeAbinash committed Dec 26, 2023
1 parent 8abeeba commit 8874899
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
55 changes: 55 additions & 0 deletions leetcode/problems/cpp/design-circular-queue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// by @codeAbinash

#include "vector"
using namespace std;

class MyCircularQueue {
private:
vector<int> 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; }
};
35 changes: 35 additions & 0 deletions leetcode/problems/cpp/maximum-score-from-removing-stones.cpp
Original file line number Diff line number Diff line change
@@ -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<int> 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;
}
};

0 comments on commit 8874899

Please sign in to comment.