• Home
  • About
    • PI photo

      PI

      Beginner's Blog

    • Learn More
    • Github
  • Posts
    • All Posts
    • All Tags
    • All Categories
  • Projects

[BAEKJOON] 백준 12789 도키도키 간식드리미 문제 풀이

📆 Created: 2025.03.05 Wed

Reading time ~1 minute

12789 도키도키 간식드리미

출처: https://www.acmicpc.net/problem/12789
문제

문제 이해

  1. numbers를 앞에서부터 하나씩 본다.
  2. 현재 number가 가장 작은 수 p인지 확인한다
  3. 같다면 p를 1 증가시키고 다음 number를 확인한다
  4. 다르다면 stack의 마지막 수랑 p를 비교한다
    1. 같다면 stack의 마지막 수를 빼주고 p를 1증가시키고 다음 마지막 수와 비교한다
    2. 다르다면 반복을 종료한다
  5. 그림1
  6. 그림2
  7. 그림3
  8. 그림4
  9. 그림5
  10. 그림6
  11. 그림7
  12. 그림8

최종 정답

import sys
from typing import *

input = sys.stdin.readline

N:int = int(input())
numbers:List[int] = list(map(int, input().split()))
stack:List[int] = []
p:int = 1

for n in numbers:
    if n == p: # 현재 number가 p랑 같다면
        p += 1
        continue
    else: # 다르다면면
        while(stack): # stack이 채워져있다면
            if stack[-1] == p: # stack의 마지막 원소가 p와 같다면
                stack.pop() # stack 마지막 원소 빼주기
                p += 1
            else: # 다르다면
                break # 반복 안 하기
        
        stack.append(n)

while(stack):
    if stack[-1] == p:
        stack.pop()
        p += 1
    else:
        break

if stack: # stack을 다 못 빼냈다 = 승환이는 간식을 받지 못한다
    print("Sad")
else:
    print("Nice")


ALGORITHMBAEKJOONPYTHON Share Tweet +1
/#disqus_thread