[백준 1874번 파이썬] 스택 수열
import sys
input=sys.stdin.readline
n=int(input())
stack=[]
result=[]
i=1
tmp=1
for _ in range(n):
nn=int(input())
while i<=nn:
stack.append(i)
result.append('+')
i+=1
if stack[-1]==nn:
stack.pop()
result.append('-')
else:
print('NO')
tmp=0
break
if tmp!=0:
print(*result)
문제 이해가 오래걸렸음
스택에 미리 1부터 n까지 넣어두고 print(+),print(-) 로 처음에 구현했었는데
‘no’만 나와야할 때를 생각하여 수정하였음.
스택에 미리 n까지 넣어두지 않고 만들고자 하는 수열의 수가 첫 등장 전까지의 수들을
스택에 append 하는 식으로 구현하는 것으로 수정.