백준 링크
import sys
N=int(sys.stdin.readline())
stack=[]
result=[]
output=[]
for i in range(N):
result.append(int(sys.stdin.readline()))
head=0
for i in range(1,N+1):
stack.append(i)
output.append('+')
if stack[-1]==result[head]:
while stack[-1]==result[head]:
stack.pop()
output.append('-')
head+=1
if stack==[]:
break
if output.count('-')==output.count('+'):
for i in output:
print(i)
else:
print("NO")
- 스택을 이용한 풀이!
- pop과 push(append)를 통해서 비교를 계속해서 해나감.
import sys
N=int(sys.stdin.readline())
stack=[]
result=[]
output=[]
for i in range(N):
result.append(int(sys.stdin.readline()))
head=0
for i in range(1,N+1):
stack.append(i)
output.append('+')
if stack[-1]==result[head]:
while stack[-1]==result[head]:
stack.pop()
output.append('-')
head+=1
if stack==[]:
break
if output.count('-')==output.count('+'):
print(*output)
else:
print("NO")