-
[Python] 백준 - 1874 스택 수열__Python/__Algorithm 2021. 12. 20. 19:59
# 백준 Silver3
# 1874 스택 수열
[문제]
링크 : https://www.acmicpc.net/problem/1874
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
[문제 풀이]
스택
[구현 코드]
import sys N = int(sys.stdin.readline()) target = list(int(sys.stdin.readline()) for _ in range(N)) s = [] ans = '' for i in range(1,N+1): s.append(i) ans += "+" while(s): if s[-1] == target[0]: s.pop() ans += "-" del target[0] else: break print("NO") if s else print(*ans,sep='\n')
[실행 결과]
728x90'__Python > __Algorithm' 카테고리의 다른 글
[Python] 백준 - 1003 피보나치 함수 (0) 2021.12.21 [Python] 백준 - 11047 동전 0 (0) 2021.12.20 [python] 백준 - 1654 랜선자르기 (0) 2021.12.20 [python] 프로그래머스 - 87946 피로도 (0) 2021.11.16 [python] 프로그래머스 - 12906 같은 숫자는 싫어 (0) 2021.10.05