[백준 문제풀이] 12789 - 제로

백준 문제풀이 12789번

풀이

첫 번째 풀이 때 기다리는 공간에서도 갈 수 있다는 걸 간과하고 틀렸다. 스택을 구현하고 스택의 값과 받은 번호표의 값을 순서값(count)으로 비교하고 출력하면 끝이다.

코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import sys

input = sys.stdin.readline

N = int(input())
numbers = list(map(int, input().split(" ")))
wait = []
count = 1  # 올바른 순서를 위한 카운트

while numbers: # 빌 때까지 반복
    if numbers[0] == count:
        numbers.pop(0)
        count += 1
    else:
        wait.append(numbers.pop(0))
    
    while wait:
        if wait[-1] == count:
            wait.pop()
            count += 1
        else:
            break

if not wait:
    print("Nice")
else:
    print("Sad")

댓글남기기