Algorithm/백준

[백준] 도키도키 간식드리미(12789) - 파이썬

potato_pizza 2024. 6. 21. 16:04
728x90

도키도키 간식드리미

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

 

문제

코드

  • 현재 간식 받을 순서인 turn 을 활용
  • 차례로 stack에 쌓고, stack의 마지막 부분이 현재 받을 순서라면 pop
  • stack에 남아있는 숫자가 있다면 Sad, 없으면 Nice 출력
# 도키도키 간식드리미
# 12789

import sys
input = sys.stdin.readline

N = int(input())

num = list(map(int, input().split()))

stack = []
turn = 1 # 지금 간식 받는 순서

for i in num:
    stack.append(i)

    while stack and stack[-1] == turn:
        stack.pop()
        turn += 1

if stack:
    print("Sad")
else:
    print("Nice")
728x90
반응형