๋ฌธ์ ์ค๋ช
์ด์ค ์ฐ์ ์์ ํ๋ ๋ค์ ์ฐ์ฐ์ ํ ์ ์๋ ์๋ฃ๊ตฌ์กฐ๋ฅผ ๋งํฉ๋๋ค.
๋ช ๋ น์ด์์ ํ(๋์ด)I ์ซ์ | ํ์ ์ฃผ์ด์ง ์ซ์๋ฅผ ์ฝ์ ํฉ๋๋ค. |
D 1 | ํ์์ ์ต๋๊ฐ์ ์ญ์ ํฉ๋๋ค. |
D -1 | ํ์์ ์ต์๊ฐ์ ์ญ์ ํฉ๋๋ค. |
์ด์ค ์ฐ์ ์์ ํ๊ฐ ํ ์ฐ์ฐ operations๊ฐ ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง ๋, ๋ชจ๋ ์ฐ์ฐ์ ์ฒ๋ฆฌํ ํ ํ๊ฐ ๋น์ด์์ผ๋ฉด [0,0] ๋น์ด์์ง ์์ผ๋ฉด [์ต๋๊ฐ, ์ต์๊ฐ]์ return ํ๋๋ก solution ํจ์๋ฅผ ๊ตฌํํด์ฃผ์ธ์.
์ ํ์ฌํญ- operations๋ ๊ธธ์ด๊ฐ 1 ์ด์ 1,000,000 ์ดํ์ธ ๋ฌธ์์ด ๋ฐฐ์ด์ ๋๋ค.
- operations์ ์์๋ ํ๊ฐ ์ํํ ์ฐ์ฐ์ ๋ํ๋
๋๋ค.
- ์์๋ “๋ช ๋ น์ด ๋ฐ์ดํฐ” ํ์์ผ๋ก ์ฃผ์ด์ง๋๋ค.- ์ต๋๊ฐ/์ต์๊ฐ์ ์ญ์ ํ๋ ์ฐ์ฐ์์ ์ต๋๊ฐ/์ต์๊ฐ์ด ๋ ์ด์์ธ ๊ฒฝ์ฐ, ํ๋๋ง ์ญ์ ํฉ๋๋ค.
- ๋น ํ์ ๋ฐ์ดํฐ๋ฅผ ์ญ์ ํ๋ผ๋ ์ฐ์ฐ์ด ์ฃผ์ด์ง ๊ฒฝ์ฐ, ํด๋น ์ฐ์ฐ์ ๋ฌด์ํฉ๋๋ค.
๊ฝค ์ด๋ ต๋ค!!
์ฒ์์ 2, 6๋ฒ์ ํต๊ณผ๋ชปํด์ ํค๋ฉจ๋๋ฐ ๊ฒฐ๊ตญ ๊ณ ์ณค์ ใ ใ
ํ๋ ์์ ํ์์ด ๋ถ๊ฐ๋ฅํด์, ์ต๋๊ฐ ์ญ์ ๋ pop์ผ๋ก ์ฒ๋ฆฌํ๊ณ ์ต์๊ฐ ์ญ์ ๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ผ๋ก ์ฒ๋ฆฌํด์ค์ผ ํฉ๋๋ท
์ฐ์ ์์ ํ๋ฅผ ์ฌ์ฉํด์, D 1 ์ผ๋๋ ๊ทธ๋ฅ popํด์ฃผ๊ณ
D -1 ์ผ๋๋ ์ฌ์ด์ฆ๊ฐ ์์๋ ๋์ ๊ฐ์ ์ญ์ ํด์คฌ๋๋ฐ
์ฒ์์๋ ํ์ฌ ํ ์ฌ์ด์ฆ(nowSize)๋ฅผ ๊ณ ๋ คํ์ง ์๊ณ "D 1" pop์ ์งํํด์ ๋ชจ์์ด ์๊ธด ๊ฒ ๊ฐ์๋ค!!
์ด๊ฑธ ๊ณ ์ณ์ฃผ๋ ๋ค ๋ง์์ต๋๋ค ^-^
๋์ ํ์ด
#include <string>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
vector<int> solution(vector<string> operations) {
vector<int> answer;
priority_queue<int> pq;
int minDelete = 0;
int nowSize = 0;
for(int i=0; i<operations.size(); ++i){
if(operations[i]=="D 1"){
if(nowSize>0) {
pq.pop();
nowSize--;
}
}
if(operations[i]=="D -1"){
if(nowSize>0) {
minDelete++;
nowSize--;
}
}
if(operations[i][0]=='I'){
operations[i].replace(0,2,""); // ์ซ์๋ง ๋จ๊น
int num = stoi(operations[i]);
pq.push(num);
nowSize++;
}
}
if(nowSize==0 || nowSize <= minDelete) {
answer.push_back(0);
answer.push_back(0);
}
else {
int pqtop = pq.top();
while(pq.size()>minDelete+1) {
cout << pq.top()<<" ";
pq.pop();
}
answer.push_back(pqtop);
answer.push_back(pq.top());
}
return answer;
}
'๐ ์๊ณ ๋ฆฌ์ฆ > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C++/PGS] Lv.1 : ๋ฌ๋ฆฌ๊ธฐ ๊ฒฝ์ฃผ (ํด์๋งต Map) (0) | 2024.10.03 |
---|---|
[C++/PGS] Lv.2 : ๋ ๋งต๊ฒ (ํ Heap) (1) | 2023.09.19 |
[C++/PGS] Lv.2 : ์ด์ง ๋ณํ ๋ฐ๋ณตํ๊ธฐ (0) | 2023.09.17 |
[C++/PGS] Lv.0 : ๋คํญ์ ๋ํ๊ธฐ (๊ตฌํ) (0) | 2023.09.16 |
[C++/PGS] Lv.0 : ์น์์ด (1) (๊ตฌํ) (0) | 2023.05.27 |