알고리즘/스택, 큐

[백준] 1874번 : 스택 수열 #Java

VIPeveloper 2022. 5. 8. 12:44
728x90
반응형
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int N = Integer.parseInt(br.readLine());

        Stack<Integer> stack = new Stack<>();

        StringBuilder answer = new StringBuilder();

        int start = 0;

        for (int i = 0; i < N; i++) {
            int tmp = Integer.parseInt(br.readLine());

            if(tmp > start){
                for (int j = start+1; j <= tmp; j++) {
                    stack.push(j);
                    answer.append('+').append('\n');
                }
                start = tmp;
            } else if(stack.peek() != tmp){
                System.out.println("NO");
                return;
            }
            stack.pop();
            answer.append('-').append('\n');
        }

        System.out.println(answer);

    }
}
728x90
반응형