문제: 10866번: 덱
basic-algo-lecture/workbook/0x07.md at master · encrypted-def/basic-algo-lecture
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip> // setprecision을 사용하기 위한 헤더
#include <climits>
#include <list>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n; // 명령의 수
cin >> n;
deque<int> dq;
while (n-- > 0) {
string op;
cin >> op;
int x;
if (op == "push_front") {
cin >> x;
dq.push_front(x);
}
else if (op == "push_back") {
cin >> x;
dq.push_back(x);
}
else if (op == "pop_front") {
if (dq.empty())
cout << -1 << "\n";
else {
int front = dq.front();
dq.pop_front();
cout << front << "\n";
}
}
else if (op == "pop_back") {
if (dq.empty())
cout << -1 << "\n";
else {
int back = dq.back();
dq.pop_back();
cout << back << "\n";
}
}
else if (op == "size") {
cout << dq.size() << "\n";
}
else if (op == "empty") {
if (dq.empty())
cout << 1 << "\n";
else
cout << 0 << "\n";
}
else if (op == "front") {
if (dq.empty())
cout << -1 << "\n";
else
cout << dq.front() << "\n";
}
else if (op == "back") {
if (dq.empty())
cout << -1 << "\n";
else
cout << dq.back() << "\n";
}
}
return 0;
}
그냥 덱 개념만 알면 쉽게 풀 수 있는 문제다.
참고자료:
BaaaaaaaarkingDog | [실전 알고리즘] 0x07강 - 덱