Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add solution of #118 #173

Merged
merged 2 commits into from
Jul 29, 2022
Merged

Feat: Add solution of #118 #173

merged 2 commits into from
Jul 29, 2022

Conversation

Dltmd202
Copy link
Contributor

#118 문제풀이

2022-07-27
@Dltmd202

문제

소스코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

/**
 * 큐
 * https://www.acmicpc.net/problem/10845
 */
public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringBuilder sb = new StringBuilder();

    public static void main(String[] args) throws IOException {
        int n = Integer.parseInt(br.readLine());
        Queue<Integer> q = new LinkedList<>();

        for (int i = 0; i < n; i++) {
            String command = br.readLine();
            if (command.startsWith("push")){
                q.offer(Integer.parseInt(command.substring(5)));
            } else {
                if(command.equals("pop")){
                    if(!q.isEmpty()) {
                        sb.append(q.poll());
                    } else {
                        sb.append(-1);
                    }
                } else if(command.equals("size")){
                    sb.append(q.size());
                } else if(command.equals("empty")){
                    sb.append(q.isEmpty() ? 1: 0);
                } else if(command.equals("front")){
                    if(!q.isEmpty()){
                        sb.append(q.peek());
                    } else {
                        sb.append(-1);
                    }
                } else if(command.equals("back")){
                    sb.append(q.stream().reduce((a, b) -> b).orElse(-1));
                }
                sb.append("\n");
            }
        }
        System.out.println(sb.toString().trim());
    }
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Deque;

/**
 * 덱
 * https://www.acmicpc.net/problem/10866
 */
public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringBuilder sb = new StringBuilder();

    public static void main(String[] args) throws IOException {
        int n = Integer.parseInt(br.readLine());
        Deque<Integer> deque = new ArrayDeque<>();

        for (int i = 0; i < n; i++) {
            String command = br.readLine();
            if (command.startsWith("push_back")){
                deque.offerLast(Integer.parseInt(command.substring(10)));
            } else if(command.startsWith("push_front")){
                deque.offerFirst(Integer.parseInt(command.substring(11)));
            } else {
                if(command.equals("pop_front")){
                    sb.append(deque.isEmpty() ? -1 : deque.pollFirst());
                } else if(command.equals("pop_back")){
                    sb.append(deque.isEmpty() ? -1 : deque.pollLast());
                } else if(command.equals("size")){
                    sb.append(deque.size());
                } else if(command.equals("empty")){
                    sb.append(deque.isEmpty() ? 1: 0);
                } else if(command.equals("front")){
                    sb.append(deque.isEmpty() ? -1 : deque.peekFirst());
                } else if(command.equals("back")){
                    sb.append(deque.isEmpty() ? -1 : deque.peekLast());
                }
                sb.append("\n");
            }
        }
        System.out.println(sb.toString().trim());
    }
}

@Dltmd202 Dltmd202 merged commit 3ca9a71 into master Jul 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant