백준 1874 Python

Heejun Kim·2022년 5월 9일
0

Coding Test

목록 보기
1/51
# flake8 코드 스타일이 적용되어 있습니다.
import sys
input = sys.stdin.readline
N = int(input())
M = []
for _ in range(N):
    M.append(int(input()))
n = [i for i in range(N, 0, -1)]
stack = []
stack_cal = []
stack_sequence = []
# ================================================= 변수 초기화

stack.append(n.pop())
stack_cal.append('+')

# ================================================== 스택 수열 찾기
i = 0
while n:
    if not stack:
        stack.append(n.pop())
        stack_cal.append('+')

    if stack[-1] == M[i]:
        i += 1
        stack_sequence.append(stack.pop())
        stack_cal.append('-')
        continue

    stack.append(n.pop())
    stack_cal.append('+')

while stack:
    stack_sequence.append(stack.pop())
    stack_cal.append('-')

if stack_sequence == M:
    for i in stack_cal:
        print(i)
else:
    print("NO")

0개의 댓글