-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8abeeba
commit 8874899
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
35
leetcode/problems/cpp/maximum-score-from-removing-stones.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |