Updated:

1. 문제 링크

https://www.acmicpc.net/problem/10866

2. 사용 알고리즘

3. 풀이

방법 1) 덱 직접 구현

방법 2) 라이브러리의 덱 이용

4. 소스 코드

4-1. C++

https://github.com/dev-aiden/problem-solving/blob/main/boj/10866.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <deque>

using namespace std;

deque<int> d;

int main(void) {
    ios_base::sync_with_stdio(false);
    int n; for (cin >> n; n--;) {
        string cmd; cin >> cmd;
        if (cmd == "push_front") {
            int num; cin >> num;
            d.push_front(num);
        } else if (cmd == "push_back") {
            int num; cin >> num;
            d.push_back(num);
        } else if (cmd == "pop_front") {
            if (d.empty()) {
                cout << "-1\n";
            } else {
                cout << d.front() << "\n";
                d.pop_front();
            }
        } else if (cmd == "pop_back") {
            if (d.empty()) {
                cout << "-1\n";
            }
            else {
                cout << d.back() << "\n";
                d.pop_back();
            }
        } else if (cmd == "size") {
            cout << d.size() << "\n";
        } else if (cmd == "empty") {
            cout << d.empty() << "\n";
        } else if (cmd == "front") {
            if (d.empty()) cout << "-1\n";
            else cout << d.front() << "\n";
        } else if (cmd == "back") {
            if (d.empty()) cout << "-1\n";
            else cout << d.back() << "\n";
        }
    }
    return 0;
}

4-2. JAVA

https://github.com/dev-aiden/problem-solving/blob/main/boj/10866.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {

    static Deque<Integer> d = new ArrayDeque<>();

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        int n = Integer.parseInt(br.readLine());
        for(int i = 0; i < n; ++i) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            String cmd = st.nextToken();
            if(cmd.equals("push_front")) {
                d.offerFirst(Integer.parseInt(st.nextToken()));
            } else if(cmd.equals("push_back")) {
                d.offerLast(Integer.parseInt(st.nextToken()));
            } else if(cmd.equals("pop_front")) {
                if(d.isEmpty()) sb.append("-1\n");
                else sb.append(d.pollFirst()).append("\n");
            } else if(cmd.equals("pop_back")) {
                if(d.isEmpty()) sb.append("-1\n");
                else sb.append(d.pollLast()).append("\n");
            } else if(cmd.equals("size")) {
                sb.append(d.size()).append("\n");
            } else if(cmd.equals("empty")) {
                sb.append(d.isEmpty() ? 1 : 0).append("\n");
            } else if(cmd.equals("front")) {
                if(d.isEmpty()) sb.append("-1\n");
                else sb.append(d.getFirst()).append("\n");
            } else if(cmd.equals("back")) {
                if(d.isEmpty()) sb.append("-1\n");
                else sb.append(d.getLast()).append("\n");
            }
        }
        System.out.println(sb);
    }
}

Updated:

Leave a comment