백준

백준 12789 - Java

으엉어엉 2024. 9. 4. 17:15
728x90

문제

 

내 풀이


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
import java.util.StringTokenizer;

public class DokeyDokey {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());

        Queue<Integer> q = new LinkedList<>();
        Stack<Integer> s = new Stack<>();

        StringTokenizer st = new StringTokenizer(br.readLine(), " ");
        for (int i = 0; i < N; i++) {
            q.offer(Integer.parseInt(st.nextToken()));
        }

        int start = 1;

        while (!q.isEmpty()) {
            if (q.peek() == start) {
                q.poll();
                start++;
            } else if (!s.isEmpty() && s.peek() == start) {
                s.pop();
                start++;
            } else {
                s.push(q.poll());
            }
        }
        while (!s.isEmpty()) {
            if (s.peek() == start) {
                s.pop();
                start++;
            } else {
                System.out.println("Sad");
                return;
            }
        }
        System.out.println("Nice");
    }
}

 

728x90

'백준' 카테고리의 다른 글

백준 2164 - Java  (0) 2024.09.05
백준 18258 - Java  (1) 2024.09.05
백준 4949 - Java  (0) 2024.09.04
백준 10773 - Java  (0) 2024.09.04
백준 28278 - Java  (0) 2024.09.04