백준 1874 - 스택 수열

남현·2025년 12월 15일

백준

목록 보기
15/16

문제

import java.util.*;

class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Stack<Integer> st = new Stack<>();
        int start = 1;
        boolean res = true;
        StringBuilder sb = new StringBuilder();
        for(int i=0; i<n; i++) {
            int num = sc.nextInt();
            while(start <= num) {
                st.push(start++);
                sb.append("+\n");
            }
            if(!st.isEmpty() && st.peek() == num) {
                st.pop();
                sb.append("-\n");
            } else {
                res = false;
                break;
            }
        }
        sc.close();
        if(!res) {
            System.out.print("NO");
        } else {
            System.out.print(sb.toString());
        }
    }
}
profile
백엔드 호소인

0개의 댓글