728x90
1874번: 스택 수열
1부터 n까지에 수에 대해 차례로 [push, push, push, push, pop, pop, push, push, pop, push, push, pop, pop, pop, pop, pop] 연산을 수행하면 수열 [4, 3, 6, 8, 7, 5, 2, 1]을 얻을 수 있다.
www.acmicpc.net
n = int(input())
numbers = [int(input()) for _ in range(n)]
stack = []
result_list = []
num = 1
result = 0
while num <= n or stack:
if num > n:
if stack[-1] == numbers[result]:
result += 1
stack.pop()
result_list.append('-')
else:
break
elif stack and stack[-1] == numbers[result]:
result += 1
stack.pop()
result_list.append('-')
else:
stack.append(num)
num += 1
result_list.append('+')
if stack:
print("NO")
else:
print(*result_list, sep='\n')
스택 자료구조의 이해를 요하는 문제이다.
딱히 설명할게 없으니 넘어간다
'STUDY > Algorithm' 카테고리의 다른 글
[백준] 1107 리모컨 python (0) | 2021.04.28 |
---|---|
[백준] 1920 수찾기 python (0) | 2021.04.28 |
[백준] 1654 랜선자르기 python (0) | 2021.04.28 |
[백준] 1504 특정한 최단 경로 python (0) | 2021.04.27 |
[백준] 1707 이분 그래프 python (0) | 2021.04.27 |