[백준 1874 자바] 스택 수열

일단 해볼게·2023년 4월 4일
0

백준

목록 보기
114/132

https://www.acmicpc.net/problem/1874

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

public class Main {
    private static void solution() throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();    // 출력할 결과물 저장

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

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

        int start = 0;

        // N 번 반복
        while (N-- > 0) {

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

            if (value > start) {
                // start + 1부터 입력받은 value 까지 push를 한다.
                for (int i = start + 1; i <= value; i++) {
                    stack.push(i);
                    sb.append('+').append('\n');    // + 를 저장한다.
                }
                start = value;    // 다음 push 할 때의 오름차순을 유지하기 위한 변수 초기화
            }

            // top에 있는 원소가 입력받은 값과 같이 않은 경우
            else if (stack.peek() != value) {
                System.out.println("NO");
                return;
            }

            stack.pop();
            sb.append('-').append('\n');
        }

        System.out.println(sb);
    }

    public static void main(String[] args) throws Exception {
        solution();
    }
}

참고
https://infodon.tistory.com/95

profile
시도하고 More Do하는 백엔드 개발자입니다.

0개의 댓글