From 899603726be6e4c8bba6062bf50272a56f35b331 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Mon, 2 Dec 2024 17:28:00 +0000 Subject: [PATCH] =?UTF-8?q?=EC=9D=B4=EC=8A=88=20#410=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EC=86=94=EB=A3=A8=EC=85=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\354\210\234\354\234\204\355\201\220.cpp" | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 "Programmers/\354\235\264\354\244\221\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220.cpp" diff --git "a/Programmers/\354\235\264\354\244\221\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220.cpp" "b/Programmers/\354\235\264\354\244\221\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220.cpp" new file mode 100644 index 0000000..2d0d0bc --- /dev/null +++ "b/Programmers/\354\235\264\354\244\221\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220.cpp" @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include + +using namespace std; + +vector solution(vector operations) { + priority_queue maxHeap; + priority_queue, greater> minHeap; + map 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()}; +} \ No newline at end of file