Skip to content

Commit

Permalink
이슈 #410에서 솔루션 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Dec 2, 2024
1 parent b7bb5f0 commit 8996037
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions Programmers/이중우선순위큐.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <sstream>

using namespace std;

vector<int> solution(vector<string> operations) {
priority_queue<int> maxHeap;
priority_queue<int, vector<int>, greater<int>> minHeap;
map<int, int> numCount;

for (const string& op : operations) {
stringstream ss(op);
char command;
int number;
ss >> command >> number;

if (command == 'I') {
maxHeap.push(number);
minHeap.push(number);
numCount[number]++;
}
else if (!maxHeap.empty()) {
int target;
if (number == 1) {
while (!maxHeap.empty() && numCount[maxHeap.top()] == 0) {
maxHeap.pop();
}
if (!maxHeap.empty()) {
target = maxHeap.top();
maxHeap.pop();
numCount[target]--;
}
}
else {
while (!minHeap.empty() && numCount[minHeap.top()] == 0) {
minHeap.pop();
}
if (!minHeap.empty()) {
target = minHeap.top();
minHeap.pop();
numCount[target]--;
}
}
}

while (!maxHeap.empty() && numCount[maxHeap.top()] == 0) {
maxHeap.pop();
}
while (!minHeap.empty() && numCount[minHeap.top()] == 0) {
minHeap.pop();
}
}

if (maxHeap.empty() || minHeap.empty()) {
return {0, 0};
}
return {maxHeap.top(), minHeap.top()};
}

0 comments on commit 8996037

Please sign in to comment.