Playground/자바문제집

[백준] 1874번

미숫가루설탕많이 2023. 4. 6. 23:25
import java.util.*;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();   // 수열 개수
        int[] arr = new int[N]; // 수열 채울 배열
        for (int i = 0; i < N; i++) {
            arr[i] = sc.nextInt();
        }

        Stack<Integer> stack = new Stack<>();
        int num = 1;
        boolean result = true;
        StringBuilder sb = new StringBuilder();
        
        for (int i = 0; i < N; i++) {
            int suNum = arr[i];
            
            if (suNum >= num) {
                while (suNum >= num) {
                    stack.push(num++);
                    sb.append("+\n");
                }
                stack.pop();
                sb.append("-\n");
            } else {
                int n = stack.pop();
                if (n > suNum) {
                    System.out.println("NO");
                    result = false;
                    break;
                } else {
                    sb.append("-\n");
                }
            }
        }
        
        if (result) System.out.println(sb.toString());
    }
}