[백준] 1874. 스택 수열

최대 1 분 소요

1874. 스택 수열


Code

package baekjoon;

import java.util.*;
import java.io.*;

public class 스택수열_1874 {
    public static void main(String[] args) throws Exception, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());

        int[] arr = new int[n+1];
        for(int i=1; i<=n; i++)
            arr[i] = Integer.parseInt(br.readLine());

        ArrayList<Character> list = new ArrayList<>(); // 연산 부호
        Stack<Integer> stack = new Stack<>();   // 숫자

        int idx = 1;

        for(int i=1; i<=n; i++){
            stack.push(i);
            list.add('+');
            while(!stack.isEmpty() && stack.peek() == arr[idx]){
                stack.pop();
                list.add('-');
                idx++;
            }
        }

        StringBuilder sb = new StringBuilder();

        if(stack.isEmpty()){
            for(Character c : list)
                sb.append(c+"\n");
        }else
            sb.append("NO\n");

        System.out.println(sb.toString());
        br.close();
    }
}

카테고리:

업데이트:

댓글남기기