백준_포스택(25556번)

tth-k·2023년 8월 10일

0base_tth

목록 보기
2/9

문제

입출력 및 예제들

코드작성

import java.util.Scanner;
import java.util.Stack;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        int[] array = new int[num];
        Stack<Integer>[] a = new Stack[4];

        for (int i = 0; i < num; i++) {
            array[i] = sc.nextInt();
        }

        for (int i = 0; i < 4; i++) {
            a[i] = new Stack<>(); // 스택 배열a의 각각의 스택을 초기화하고
            a[i].push(0); // 0을 push
        }
		
        // 현재 item이 스택 a[i]의 맨 위의 값보다 크다면, 해당 스택에 'item'을 push
        for (int item : array) {
            for (int i = 0; i < 4; i++) {
                if (item > a[i].peek()) {
                    a[i].push(item);
                    break;
                }
                // 모든 스택에서 현재 숫자 `item`보다 작거나 같은 값을 가진 스택이 없다면, "NO"를 출력하고 프로그램 종료
                if (i == 3 && item <= a[i].peek()) {
                    System.out.println("NO");
                    return;
                }
            }
        }
        System.out.println("YES");
    }
}
profile
백엔드 취준생 / 코린이 ヾ(≧▽≦*)o

0개의 댓글